抛出异常时是删除静态对象还是仅删除本地对象?
#include <iostream>
#include <exception>
using std::cout;
using std::endl;
class test
{
public:
test()
{
cout<<"constructor called"<<endl;
}
~test()
{
cout<<"destructor called"<<endl;
}
void fun(int x)
{
throw x;
}
};
int main()
{
try
{
static test k;
k.fun(3);
}
catch(int k)
{
cout<<"exception handler"<<endl;
}
}
当抛出异常时,那么在堆栈展开过程中,我认为只有本地对象被销毁,而不是静态或堆对象。如果这是真的,我不确定为什么调用类(测试)析构函数?谢谢。
#include <iostream>
#include <exception>
using std::cout;
using std::endl;
class test
{
public:
test()
{
cout<<"constructor called"<<endl;
}
~test()
{
cout<<"destructor called"<<endl;
}
void fun(int x)
{
throw x;
}
};
int main()
{
try
{
static test k;
k.fun(3);
}
catch(int k)
{
cout<<"exception handler"<<endl;
}
}
When the exception is thrown, then during the stack unwinding process, I think only local objects are destroyed, not static or heap objects. If this is true, I am not sure why the class (test) destructor is called? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
main 退出后调用测试析构函数。
现在测试
Static(静态存储持续时间对象)在main退出后按照创建的相反顺序被销毁。
The test destructor is called after main exits.
Now testing
Static (static storage duration objects) are destroyed in the reverse order of creation after main exits.
析构函数被调用是因为您的程序正在退出。只有自动存储持续时间的对象(绝对不是堆栈对象或堆对象)才会被销毁。
The destructor is called because your program is exiting. Only objects of automatic storage duration (that is definitely not stack objects or heap objects) are destroyed.
输出
运行此代码时,我得到了有意义的 。首先调用静态
test
对象的构造函数。当抛出异常时,异常处理程序会捕获异常并打印消息。最后,当程序终止时,调用静态test
对象的析构函数。假设异常实际上在某处被捕获,异常只会导致具有自动持续时间的变量(即局部变量)的生命周期结束。异常不会破坏具有动态持续时间的对象(即使用 new 分配的对象),但如果动态分配对象的构造函数中发生异常,则内存将被回收,因为否则无法获取记忆回来了。同样,静态对象不会被销毁,因为它们应该在整个程序中持续存在。如果它们被清理掉,并且在程序中传递对这些对象的引用,则可能会导致问题。
希望这有帮助!
When running this code, I get the output
Which makes sense. The constructor for the static
test
object is invoked first. When the exception is thrown, it is caught by the exception handler and the message is printed. Finally, when the program terminates, the destructor for the statictest
object is invoked.Exceptions only cause the lifetimes of variables with automatic duration (i.e. locals) to end, assuming that the exception is actually caught somewhere. Exceptions will not destroy objects with dynamic duration (i.e. things allocated with
new
), though if an exception occurs in a constructor for a dynamically-allocated object the memory will be reclaimed, since there's otherwise no way to get the memory back. Similarly,static
objects are not destroyed, since they're supposed to last for the entire program. If they were cleaned up, it could cause problems if references to those objects had been passed around the program.Hope this helps!