Explicitly Freeing Memory

Vladimir Panteleev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 18 21:49:29 PST 2014


On Wednesday, 19 November 2014 at 03:47:04 UTC, Maxime 
Chevalier-Boisvert wrote:
> 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.

InvalidMemoryOperationError occurs when you're trying to invoke 
GC functions during a GC pass (because the current D GC is 
non-reentrant). This can occur if you explicitly free objects 
from the destructor, and the destructor is invoked by the GC.

I suppose you can work around this by using a separate function 
which deallocates owned objects' memory.

P.S. Still working on Diamond.


More information about the Digitalmars-d-learn mailing list