22

What will exactly happen if a member function try to do delete this;, like in the constructor of the following class?

class A
{
public:
    A(int);
    ~A();
    int *pi;
}

A::A(int i)
{
    delete this;
    pi = new int(i);
}

A::~A()
{
    delete pi;
}
zhanwu
  • 1,508
  • 5
  • 16
  • 27
  • Using `delete this;` is fine. Doing it in the constructor as well as accessing fields of the objects after it is undefined behavior. Sadly this doesn't typically cause a processor fault, just heap corruption. – Hans Passant Aug 12 '11 at 11:52
  • Als' answer explains the conditions very well under which this is allowed and by those conditions your code is not ok. Inside the constructor you're allowed to throw an exception. – Sebastian Aug 12 '11 at 12:05

4 Answers4

44

This C++ FAQ entry answers this quite nicely, repeating here:

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

Here's how I define "careful":

  • You must be absolutely 100% positively 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).
  • You must be absolutely 100% positively sure that your member function will be the last member function invoked on this object.
  • You must be absolutely 100% positively 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).
  • You must be absolutely 100% positively 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.

You are violating the #3 by accessing pi after delete this

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • I guess you should point out that the given example does not fulfill requirement 3 because member `pi` is accessed after `delete this` in the constructor. – Sebastian Aug 12 '11 at 12:03
  • @Sebastian: Done, Thanks for pointing that out, It is better to explicitly state it. – Alok Save Aug 12 '11 at 12:08
  • 1
    It may be `legal` but it is a **very strong** indicator that you got your design wrong. Resource management and business logic should not be intertwined. An object that manages itself is not a properly constructed object as it can not know about the greater context. It is always better to delegate the destruction to a manager. Rather than destroying yourself it would be better to register yourself for destruction with a manager object that has a greater understanding of the context. – Martin York Aug 12 '11 at 14:13
  • @Martin: I disagree. This trick is used in COM (indirectly, via use of `Release()`) to ensure that `delete` uses the same memory allocator used to allocate the object. This should definitely ring some sort of alarm when you type it, but there is at least 1 good reason to use this construct. – André Caron Aug 23 '11 at 19:50
  • @André Caro: COM is a perfect example of why it is bad design. COM is just about uniformly considered a mistake (it may have been a good idea at the time but C++ has evolved so much since then it is now considered bad). – Martin York Aug 24 '11 at 06:15
  • @LokiAstari That's your opinion, isn't it? Do you have any serious reference? – harper Feb 23 '15 at 07:39
  • @harper: You could look up the discussions of the standards committee on why intrusive pointer never made it from boost into the standard. – Martin York Feb 23 '15 at 08:32
3

Bad things. You can delete this; but it's generally speaking an extremely bad idea, and once it's done, you cannot touch any member variables or member functions.

Puppy
  • 144,682
  • 38
  • 256
  • 465
1

In general, invoking delete this from inside a member function is well-defined, if a little risky.

But it's probably a very bad idea to invoke it from the constructor (I don't know if it's well-defined). Why would you ever want to do that?

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

You can delete this, but this assumes that the instance was created with new and that you don't access any members of the instance any more.

In your example, pretty much the same would happen if you did

class A
{
public:
    A(int);
    int *pi;
};

A::A(int i)
{
    pi = new int(i);
}

int main()
{
   A* p = new A(10);
   delete p;
   p->pi = new int; //bad stuff

//or
   A a(20);
   delete &a;  //bad stuff
   a.pi = new int; //more bad stuff
}

And even more bad stuff happens because when you delete this, the pi member is unitialized, leading to destructor attempting to delete a dangling pointer.

visitor
  • 1,781
  • 10
  • 7