Should destructors be able to tell who called them?

Sean Kelly sean at invisibleduck.org
Tue Aug 10 11:51:56 PDT 2010


Steven Schveighoffer Wrote:

> One of the common problems of destructors is that they cannot assume any  
> of their GC-allocated resources are valid when inside the destrutor.

Just a note.  It's already possible to do this in D2 using an interface.  Here's a thread about it:

http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=89443&header

And some sample code:

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;
}


More information about the Digitalmars-d mailing list