D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6821 - core.exception.OutOfMemoryError on dtor field test of class-embedded struct
Summary: core.exception.OutOfMemoryError on dtor field test of class-embedded struct
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-16 20:37 UTC by Andrej Mitrovic
Modified: 2012-01-04 06:56 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2011-10-16 20:37:24 UTC
struct Bar
{
    this(int x) {}
    
    uint _count;

    ~this()
    {
        assert(this._count > 0);
    }        
}

class Foo
{
    this()
    {
        bar = Bar(1);
    }
    
    Bar bar;
}

void main()
{
    auto foo = new Foo();
}

$ dmd test.d && test.exe
$ core.exception.OutOfMemoryError

The "assert(this._count > 0);" triggers this exception. I can only recreate this if "bar" is a field of class Foo, and not just a temporary inside Foo's constructor or anywhere else.

I'm labeling this as critical since these checks are prevalent throughout the CairoD library and this seems like some kind of memory corruption issue.

I've also had this bug appear and disappear based on the current path of invocation of an executable, IOW sometimes an app would throw this exception on exit if it was invoked on a directory UP from the current application directory, while in all other cases the exception would not be thrown.
Comment 1 Andrej Mitrovic 2011-10-16 20:39:37 UTC
> I've also had this bug appear and disappear based on the current path of
> invocation of an executable, IOW sometimes an app would throw this exception on
> exit if it was invoked on a directory UP from the current application
> directory, while in all other cases the exception would not be thrown.

When I say "an" executable I mean a much more complicated app than this small test case.
Comment 2 yebblies 2011-10-17 03:10:30 UTC
I'm guessing the issue here is:

Currently attempting to allocate during a garbage collection throws an oom error.  This is due to limitations in the current gc implementation, previously it just corrupted memory.  As the Foo object is only destroyed when the final collection occurs, the assert failing in the destructor tries to allocate an AssertError and fails.
Comment 3 Andrej Mitrovic 2011-10-17 09:03:17 UTC
Because AssertError is an object, I understand. I think it was Vladimir who changed it to throw an exception, which is a good thing rather than have it corrupt memory. I guess this isn't so critical then, although a note of what you shouldn't be doing in a dtor should be added to the docs.
Comment 4 Andrej Mitrovic 2012-01-04 06:44:01 UTC
I think it's ok to close this, this is a GC implementation issue and is not necessarily a bug.
Comment 5 Vladimir Panteleev 2012-01-04 06:56:21 UTC
(In reply to comment #3)
> Because AssertError is an object, I understand. I think it was Vladimir who
> changed it to throw an exception, which is a good thing rather than have it
> corrupt memory.

Yup. The newer Druntime versions have a dedicated exception class for this error (InvalidMemoryOperationError), which should be Google-able enough to clear confusion regarding it.