Manual memory management in D2

Simen kjaeraas simen.kjaras at gmail.com
Sun Jul 11 01:45:12 PDT 2010


Petr <janda.petr at gmail.com> wrote:

> 1) Do i do clear(c) and then GC.free(c)?

Yes.


> What would happen if i skipped clear()?

Then your destructors wouldn't get called. GC.free takes a humble void
pointer, which knows little of destructors and other such fancy things.

If you feel unsure you will remember, feel free to use this function:

void destroy( T )( T obj ) if ( is( T == class ) ) {
     clear( obj );
     GC.free( obj );
}


> 2) What is D's equivalent of C++ std::memory? If there's none, what are  
> the implications of using C's malloc and free
> in D as opposed to in C, if any?

There isn't really one - there is core.memory, but that's mostly just a
shim atop the GC.

If you choose to use malloc and free, you yourself are responsible for
cleaning up, and for registering things with the GC.

There is also GC.malloc, which does basically the same. It has some bells
on it, but I know not for sure what they do.

-- 
Simen


More information about the Digitalmars-d mailing list