I'm not deleting the member. I'm deleting memory, allocated by the member. If GC deleted it before the object, the same error would appear when I forced a GC collection cycle. Also, docs clearly say, that I'm free to delete the memory myself. This means, that I shouldn't care if a collection cycle went before my deletion!
On Wed, May 9, 2012 at 8:07 PM, Steven Schveighoffer <[email protected]> wrote: > 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 -- Bye, Gor Gyolchanyan.
