Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > destructor / semantics of delete this

Reply
Thread Tools

destructor / semantics of delete this

 
 
Alexander Stippler
Guest
Posts: n/a
 
      08-20-2003
Hi,

I have some question about the usage of delete. I have some reference
counted classes, all derived from SharedObject. It's destructor looks like
this:

~SharedObject()
{
if (--references_ == 0) {
delete this;
}
}

Now I'd like to know if
a) it's OK to use delete this (especially in the destructor)
and
b) my guess, about what happens, is correct.
In my understanding the destructor is entered and delete this is executed
(if "if" is entered of course). Delete, as far as I know, calls the
destructor of the object again, now the if-condition is false and the
storage is freed due to the first call of delete.
I may be absolutely wrong. I would not wonder. But what does really happen
and is it a good idea at all to use delete this.

regards,
alex
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      08-20-2003
"Alexander Stippler" <> wrote in message
news:...
| I have some question about the usage of delete. I have some reference
| counted classes, all derived from SharedObject. It's destructor looks like
| this:
|
| ~SharedObject()
| {
| if (--references_ == 0) {
| delete this;
| }
| }

This won't work. Once the destructor is executed, it is too late to
try to cancel the destruction process.
You need to use a different approach:
class SharedObject
{
public:
SharedObject() : refCnt(1) {}
void addRef() { ++refCnt; }
void release() { if(!--refCnt) delete this; }
protected:
virtual ~SharedObject()=0; // abstract class
private:
int refCnt;
};
SharedObject::~SharedObject() {}

Such a base class is usually combined with a smart pointer.
+ templates are to be considered for type safety.

Note that reference-counted classes is a well studied subject in C++.
Make sure to read advice from Scott Meyers and others on the subject.
Also, existing implementations such as boost::shared_ptr definitely
are to be considered:
http://www.boost.org/libs/smart_ptr/shared_ptr.htm


| Now I'd like to know if
| a) it's OK to use delete this (especially in the destructor)
Yes in rare and specific cases -- but never in the destructor.

| b) my guess, about what happens, is correct.
| In my understanding the destructor is entered and delete this is executed
| (if "if" is entered of course). Delete, as far as I know, calls the
| destructor of the object again, now the if-condition is false and the
| storage is freed due to the first call of delete.
This leads to undefined behavior. Not only is it illegal to call the
destructor twice. But also, the call to 'delete' will attempt to free
memory (=> the same memory block will be freed twice, or the call
will attempt to free a stack-based address... not good at all).


I hope this helps,


 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      08-20-2003

"Alexander Stippler" <> wrote in message
news:...
> Hi,
>
> I have some question about the usage of delete. I have some reference
> counted classes, all derived from SharedObject. It's destructor looks like
> this:
>
> ~SharedObject()
> {
> if (--references_ == 0) {
> delete this;
> }
> }
>
> Now I'd like to know if
> a) it's OK to use delete this (especially in the destructor)
> and
> b) my guess, about what happens, is correct.
> In my understanding the destructor is entered and delete this is executed
> (if "if" is entered of course). Delete, as far as I know, calls the
> destructor of the object again, now the if-condition is false and the
> storage is freed due to the first call of delete.
> I may be absolutely wrong. I would not wonder. But what does really happen
> and is it a good idea at all to use delete this.
>

As it's already been pointed out by Ivan it's not a good idea to use delete
this in a dtor. You will end up in a recursive call of the dtor which stops
after 2 iterations but still it's illegal to call the dtor of an object
twice. I'd recommend to read Scott Meyer's article from April 1998 in the
C++ Users Journal and there are countless others covering this topic. You
can furthermore refer to the FAQ 16.21 for a brief overview.

HTH
Chris


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-20-2003
Chris Theis wrote:

> As it's already been pointed out by Ivan it's not a good idea to use
> delete this in a dtor. You will end up in a recursive call of the dtor
> which stops after 2 iterations but still it's illegal to call the dtor
> of an object twice.


Not only will the destructor be called twice, but the memory will be
released twice, unless the object wasn't dynamically allocated. I this
case, delete is called for an object that never came from new.
While, depending on the situation, some compilers may let you go away
with two destructor calls, the other thing almost always results in a
crash. Generally, none of those things are allowed in C++.

 
Reply With Quote
 
Agent Mulder
Guest
Posts: n/a
 
      08-20-2003
CT> I'd recommend to read Scott Meyer's article from April 1998 in the
CT> C++ Users Journal.

You can find it here:

http://www.cuj.com/documents/s=8066/cuj9804meyers/


 
Reply With Quote
 
Alexander Stippler
Guest
Posts: n/a
 
      08-20-2003
> I hope this helps,

It helped a lot. Thanks
 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      08-20-2003

"Rolf Magnus" <> wrote in message
news:bhvluh$gn1$04$...
> Chris Theis wrote:
>
> > As it's already been pointed out by Ivan it's not a good idea to use
> > delete this in a dtor. You will end up in a recursive call of the dtor
> > which stops after 2 iterations but still it's illegal to call the dtor
> > of an object twice.

>
> Not only will the destructor be called twice, but the memory will be
> released twice, unless the object wasn't dynamically allocated. I this
> case, delete is called for an object that never came from new.


You're of course right on this point, which I should have mentioned for
completeness.

> While, depending on the situation, some compilers may let you go away
> with two destructor calls, the other thing almost always results in a
> crash. Generally, none of those things are allowed in C++.


If I get you correctly you indicate that there might be situations
("...almost always...") when calling delete twice on an object will not
result in a crash. Just out of curiosity could you give me an example.

Regards
Chris


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 2 05-20-2008 02:20 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
Destructor semantics Dilip C++ 9 09-28-2006 12:01 AM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM



Advertisments