Newbie initial comments on D language - destructor usage

Edward Diener eddielee_no_spam_here at tropicsoft.com
Wed Jan 30 15:21:14 PST 2008


James Dennett wrote:
> Edward Diener wrote:
>> Janice Caron wrote:
>>> On Jan 29, 2008 12:52 AM, Edward Diener
>>> <eddielee_no_spam_here at tropicsoft.com> wrote:
>>>> Except for a
>>>> 'scope' class a destructor can not possibly know how it is being called
>>>
>>> True.
>>>
>>> Nonetheless, I have seen code in C++ which does:
>>>
>>>     ~MyHeapClass()
>>>     {
>>>         delete this;
>>>     }
>>>
>>> even though the destructor cannot possibly know whether or not the
>>> class was allocated with new. 
>>
>> I completely agree the above is bad C++ code, and can only be possibly 
>> considered correct if some rule says that the class can never be 
>> created except in dynamic memory. 
> 
> Not even then; "delete this" is not valid from within a destructor,
> as the destructor has already set about destroying the object, and
> so attempting to re-destroy the object makes no sense.

You are completely correct and it was my error also in not noticing that 
this was done in the destructor itself.



More information about the Digitalmars-d mailing list