Destructors and Deterministic Memory Management

Nick B nick.barbalich at gmail.com
Tue May 5 03:19:28 PDT 2009


Sean Kelly wrote:
> 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 )
>     {
>         d.dispose();
>         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.

I raised this enhancement request, below :
http://d.puremagic.com/issues/show_bug.cgi?id=2757

Would this sample code solve these resource management / deterministic 
memory management issues ?




More information about the Digitalmars-d mailing list