Explicitly Freeing Memory

Maxime Chevalier-Boisvert via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 18 19:47:03 PST 2014


I posted a thread the other day explaining that I was running 
into a memory leak issue which is very hard to debug. There seems 
to be a false pointer somewhere, and I have no way of knowing 
where that is or which object is being pointed to. I decided to 
take the easy way out and explicitly free memory when I don't 
need it. Unfortunately, this brings about more problems.

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.

Not quite sure where to go from here... I guess I'd have to 
allocate all of the objects I want to free outside of GC'd space, 
but D doesn't seem to provide a convenient syntax for doing so. 
The alternate solution I'm considering is to have a special 
function to destroy all the objects owned by my parent object 
before destroying the parent object itself, and calling this 
function explicitly.


More information about the Digitalmars-d-learn mailing list