Garbage Collection, Allocators/Deallocators and

Simen kjaeraas simen.kjaras at gmail.com
Sat Sep 18 14:27:03 PDT 2010


Ivo Kasiuk <i.kasiuk at gmx.de> wrote:

>> > Exploring the example a bit further:
>> > If C's malloc is used instead of GC.malloc then the deallocators also
>> > are not called and the program runs out of memory. How are the objects
>> > supposed to get finalized in this case - do I have to use the delete
>> > keyword explicitly?
>>
>> If you use C's malloc, you will also have to use C's free.
>
> Yes, obviously. But the deallocator which contains the call to C's free
> has to be invoked by someone, otherwise it's useless. So that is my
> question: how should finalization and deallocation get triggered in this
> scenario (where non-GC memory is used)?

delete will, as long as it is kept in D, call the deallocator method.
Unless the allocated memory is not registered with the garbage collector,
it will be ignored by the garbage collector, and you will have to manually
delete allocated objects. The point of using C's malloc and free is
exactly that - the GC will ignore your objects, and you are free to (that
is, have to) manage their lifetimes yourself.

-- 
Simen


More information about the Digitalmars-d-learn mailing list