The problem with the D GC

Sean Kelly sean at f4.ca
Tue Jan 9 05:44:39 PST 2007


Lutger wrote:
> Luís Marques wrote:
>> Sean Kelly wrote:
>>> It's a modified version of the DMD GC.  The "don't scan blocks 
>>> containing elements smaller than pointer size" feature is built-in, 
>>> and there is user-level control of that behavior on a per-block 
>>> basis, among other things.  But it's still the same old mark/sweep GC 
>>> at heart.
>>
>> Does the new GC allow setting a hook to be informed of when a given 
>> object was collected? (I need that)
> 
> This is possible with these functions in Object:
> final void notifyRegister(void delegate(Object) dg);
> final void notifyUnRegister(void delegate(Object) dg);

This option is available.  There is also a global hook that can be 
called when an object is collected by the GC (as opposed to destroyed 
deterministically via delete):

     bool myCollectHandler( Object o )
     {
         if( o.classinfo.name == "MyObject" )
         {
             (cast(MyObject) o).dispose();
             return false;
         }
         return true;
     }

     setCollectHandler( &myCollectHandler );

Returning false from the hook tells the GC not to finalize the 
object--ie. destroy its monitor but don't call its dtor.  The purpose of 
this method is twofold: to detect when memory is "leaked" and to allow 
objects to be cleaned up differently if disposed via delete than via a 
GC collection.


Sean



More information about the Digitalmars-d mailing list