将exit()或异常阻止范围内的析构函数被调用吗? [英] Will exit() or an exception prevent an end-of-scope destructor from being called?

查看:67
本文介绍了将exit()或异常阻止范围内的析构函数被调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说下面的代码:

  struct mytype 
{
〜mytype {/ *做一些调用Mix_CloseAudio等* /}
};

int main()
{
mytype instant;

init_stuff();

start();

return 0;
}

是否确保析构函数被调用,即使exit()如果你调用 exit ,析构函数将不会被调用。



从C ++标准(§3.6.1/ 4):


调用函数




  void exit(int)在< cstdlib>中声明的




(18.3)终止程序而不离开当前块,因此不会破坏具有自动存储持续时间(12.4)的任何对象。如果在具有静态存储持续时间的对象销毁期间调用exit来结束程序,则程序具有未定义的行为。



Let's say I have the following code:

struct mytype
{
    ~mytype() { /* do something like call Mix_CloseAudio etc */ }
};

int main()
{
    mytype instant;

    init_stuff();

    start();

    return 0;
}

Is that destructor guaranteed to be called even if exit() is used from somewhere inside start() ?

解决方案

If you call exit, the destructor will not be called.

From the C++ standard (§3.6.1/4):

Calling the function

void exit(int);

declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior.

这篇关于将exit()或异常阻止范围内的析构函数被调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆