Destructors and Deterministic Memory Management

Sean Kelly sean at
Mon May 4 21:55:37 PDT 2009

dsimcha wrote:
> Two closely related topics here:
> 1.  It is often nice to be able to create a single object that works with both
> GC and deterministic memory management.  The idea is that, if delete is called
> manually, all sub-objects would be freed deterministically, but the object
> could still safely be GC'd.  Since the destructor called by the GC can't
> reference sub-objects, would it be feasible to have two destructors for each
> class, one that is called when delete is invoked manually and another that is
> called by the GC?

You can do this today with both Druntime on D 2.0 and Tango on D 1.0, 
though it isn't the most performant approach.  The code would look 
something like this:

import core.runtime;

interface Disposable
     void dispose();

bool handler( Object o )
     auto d = cast(Disposable) o;

     if( d !is null )
         return false;
     return true;

static this()
     Runtime.collectHandler = &handler;

If you return false from your collectHandler then the runtime won't call 
the object's dtor.

More information about the Digitalmars-d mailing list