On Wed, 09 May 2012 11:28:30 -0400, Gor Gyolchanyan <[email protected]> wrote:

I have a structure:

private struct Block
{
        this(size_t n) { /* allocate n bytes with GC.malloc */ }
        this(this) { /* deep-copy the bytes */ }
        ~this() { /* deallocate them with GC.free */ }
}

And a class:

final class Region
{
        private Block _block;
        alias _block this;
}


This setup allows me to have memory regions, reallocation of which
will never invalidate pointers, because thanks to Region class no-one
holds a direct pointer to the memory.
The problem is, that I get a
core.exception.InvalidMemoryOperationError when my program ends.

shared static ~this() { import core.thread;
Thread.sleep(dur!`seconds`(1)); } // The error message still showed up
after a delay, so it had to be at the process termination
When I delete the Region object with "clear", the error diappears.
Disabling the GC or forcing a collection before the 1-second sleep
doesn't do anything: I still get the error after the 1-second sleep.
The only way to stop the error, besides manually deleting the object
is the remove the deallocation from the Block's destructor.

Can somebody please help me fix this problem?

Yes, you cannot use GC.delete on a member of a class. Ever. The reason is, the memory you are attempting to delete may already be gone, there is no guaranteed order of destruction in a collection cycle.

-Steve

Reply via email to