今天在学习如何控制函数只在堆上或者只在栈上创建函数的时候碰巧碰到了这个问题,现象之前自己写程序的时候也遇到过这样的问题,只是直接写了delete this并没有去深入的探讨背后的问题。
使用delete关键字在销毁对象的时候会调用对象的析构函数,而delete语句又包括在析构函数之内,这样为什么不会形成死锁?delete之后怎么防止访问非法内存?
下面是isocpp针对这个问题的回答:Is it legal (and moral) for a member function to say delete this?
As long as you’re careful, it’s okay (not evil) for an object to commit suicide (delete
this
).
Here’s how I define “careful”:
- You must be absolutely 100% positively sure that
this
object was allocated vianew
(not bynew[]
, nor by placementnew
, nor a local object on the stack, nor a namespace-scope / global, nor a member of another object; but by plain ordinarynew
). - 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 ofthis
object (including calling any other member functions or touching any data members). This includes code that will run in destructors for any objects allocated on the stack that are still alive. - You must be absolutely 100% positively sure that no one even touches the
this
pointer itself after thedelete
this
line. In other words, you must not examine it, compare it with another pointer, compare it withnullptr
, 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.
大意就是:
- 确定该对象是由new关键字分配的
- 确保运行delete new的是最后一个使用this指针的成员函数
- 确保在delete this运行之后的其他成员函数(包括在析构函数内用于处理栈上分配内存的代码),不会使用this对象
- 确保不会使用this指针(包括对它进行检查、比较等操作)
在应用于this指针指向基类(base class)但是有没有虚析构函数(virtual destructor)的情况下通常会有警告。
BTW,这也是为什么在有继承的情况下,析构函数通常是虚函数