Can't free memory on exiting?

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Oct 31 21:31:16 PDT 2011


I've had something similar happen, yebblies explained it best:

http://d.puremagic.com/issues/show_bug.cgi?id=6821

For that issue it was:

"Currently attempting to allocate during a garbage collection throws an oom
error.  This is due to limitations in the current gc implementation, previously
it just corrupted memory.  As the Foo object is only destroyed when the final
collection occurs, the assert failing in the destructor tries to allocate an
AssertError and fails."

You should use core.stdc.stdlib.malloc if you want to use the C
runtime and manage memory manually, not GC.malloc which uses the
garbage collector. IOW:

import std.stdio;
import core.stdc.stdlib;

class ExternalMemoryHandle {
       void *p;

       this() {
               p = malloc(1024);
       }

       ~this() {
               free(p);
       }
}

void main() {
       auto test = new ExternalMemoryHandle();
       writeln("exiting");
}

Otherwise if you want to use the GC there's no point in calling
GC.free in the destructor since the GC will do that automatically.


More information about the Digitalmars-d-learn mailing list