destructors

Daniel Keep daniel.keep.lists at gmail.com
Tue May 1 02:39:35 PDT 2007



Bill Baxter wrote:
> Is this ok in D?
> 
> 
> class MyClass
> {
>    OtherClass a;
>    this() {
>       a = new OtherClass;
>    }
>    ~this() {
>       delete a;
>    }
> }
> 
> I was just trying to fix the crash on exit in the tinyXPath code on
> dsource, and it does the above a lot.
> 
> --bb

I'd be suspect of the delete; unless you explicitly delete an object,
you cannot guarantee in what order objects will be destroyed.  That
means that it's entirely possible that 'a' has already been deleted by
the time your dtor runs.

<Insert song and dance about how having an implicit/explicit flag passed
to dtors would solve this.>

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/


More information about the Digitalmars-d-learn mailing list