<分区>
我刚刚有个面试题,面试官问
How to tell the destructor is not called when it should have be called?
And what will you do if the destructor is not called?
老实说,我不知道答案。我的猜测是将析构函数放在 try catch
block 中,但我从未见过有人这样做。有更好的解决方案吗?
<分区>
我刚刚有个面试题,面试官问
How to tell the destructor is not called when it should have be called?
And what will you do if the destructor is not called?
老实说,我不知道答案。我的猜测是将析构函数放在 try catch
block 中,但我从未见过有人这样做。有更好的解决方案吗?
最佳答案
对象的析构函数调用失败有多种方式:
abort
或_exit
(即使是exit
也不会破坏堆栈变量)。new []
分配的数组上调用 delete
是调用未定义行为的一种方式,一个常见的行为是只调用第一个对象的析构函数(留下第二个和随后未破坏) - 但它仍然是未定义的行为。new
的指针上调用 delete
(如果您有内存泄漏,这尤其成问题)。 (这实际上是一个特别常见的“析构函数不应该被运行”的情况)。如果你正在尝试调试一个程序并想知道是否正在调用析构函数,那么
关于c++ - 如何告诉析构函数没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34232236/