destructors

Daniel Keep daniel.keep.lists at gmail.com
Tue May 1 20:18:16 PDT 2007



Bill Baxter wrote:
> Daniel Keep wrote:
>>
>> Daniel Giddings wrote:
>>> Daniel Keep wrote:
>> The other thing I started doing in some of my code was to use the
>> following:
>>
>> interface Disposable
>> {
>>     void dispose();
>> }
>>
>> void dispose(Object obj)
>> {
>>     if( auto dobj = cast(Disposable)obj )
>>         dobj.dispose();
>> }
> 
> Why not follow up the call to dispose with a 'delete dobj'?
> Would that make 'scope' fail?
> 
> 
> --bb

Then you'd get a double-free, so I think it might.  I actually forgot
about the *other* function I use:

void destroy(inout Object obj)
{
    dispose(obj);
    delete obj;
}

So you can destroy heap allocated objects, and just dispose scoped ones.

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