Explicitly Freeing Memory

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 18 20:43:39 PST 2014


On Wed, Nov 19, 2014 at 03:47:03AM +0000, Maxime Chevalier-Boisvert via Digitalmars-d-learn wrote:
[...]
> I'm trying to explicitly free chunks of memory allocated with
> GC.malloc() in a destructor. This works fine while the program is
> running, but when the program terminates, it seems the GC calls all
> destructors in an arbitrary order. I then get a
> core.exception.InvalidMemoryOperationError because I'm trying to free
> memory that is already freed.
[...]

Yeah, it is well-known that relying on destructor calling order is a bad
idea. The GC makes no guarantees about what order destructors will get
called, if they get called at all. Also, calling GC allocation /
deallocation functions from inside a dtor is also a bad idea, since the
GC is not reentrant.

Unfortunately I don't have any good suggestions... I have been avoiding
depending on dtors in D because of the aforementioned issues (and more),
so I haven't had much experience in debugging dtor-related problems in
D.


T

-- 
An elephant: A mouse built to government specifications. -- Robert Heinlein


More information about the Digitalmars-d-learn mailing list