Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "delete this" a bad idea? [duplicate]

Possible Duplicate:
Is it safe to delete this?

I've been doing a little work on a class that's designed to act as a node in a linked list, and I figured I'd give the class its own deletion function as opposed to the managing class doing it. So basically it goes like this:

void Class::Delete() {
    //Some cleanup code before deleting the object
    delete this;
}

Now I've tested this and it appears to work fine, but I've had a problem in the past where objects have been in the middle of running code, been deleted, then obviously crashed the program by trying to use a no-longer-existing object.

Since "delete this" is right at the end of the function, it obviously exits the function and works fine, but is this sort of practice a bad idea at all? Could this ever blow up in my face if I'm not careful?

like image 418
Linkage Avatar asked Jun 14 '11 05:06

Linkage


People also ask

Is delete this valid?

“delete this” in C++Ideally delete operator should not be used for this pointer. However, if used, then following points must be considered. 1) delete operator works only for objects allocated using operator new (See this post).

What happens if you delete the same memory a second time?

Deleting the same memory twice is undefined behaviour. Anything may happen, including nothing. It may e.g. cause a crash sometime later.

Can I call delete this?

It's safe to delete "this" as long as it's essentially the last operation in the method. In fact several professional level APIs do so (see ATL's CComObject implementation for an example). The only danger is attempting to access any other member data after calling "delete this". This is certainly unsafe.

What is a double delete?

Double-deletion is when you delete an email message from your inbox and then immediately delete it from the trash folder as well.


1 Answers

The FAQlite answers this quite well:

As long as you're careful, it's OK for an object to commit suicide (delete this).

Here's how I define "careful":

  1. You must be absolutely 100% positive sure that this object was allocated via new (not by new[], nor by placement new, nor a local object on the stack, nor a global, nor a member of another object; but by plain ordinary new).
  2. You must be absolutely 100% positive sure that your member function will be the last member function invoked on this object.
  3. You must be absolutely 100% positive sure that the rest of your member function (after the delete this line) doesn't touch any piece of this object (including calling any other member functions or touching any data members).
  4. You must be absolutely 100% positive sure that no one even touches the this pointer itself after the delete this line. In other words, you must not examine it, compare it with another pointer, compare it with NULL, print it, cast it, do anything with it.

Naturally the usual caveats apply in cases where your this pointer is a pointer to a base class when you don't have a virtual destructor.

Basically, you need to take the same care as you do with deleteing any other pointer. However, there are more areas where things can go wrong with a member function committing suicide, compared with an explicitly-declared pointer.

like image 169
Dominic Gurto Avatar answered Oct 13 '22 11:10

Dominic Gurto