c++ - exit() 或异常会阻止调用范围结束的析构函数吗?

标签 c++ destructor exit

假设我有以下代码:

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

int main()
{
    mytype instant;

    init_stuff();

    start();

    return 0;
}

即使从 start() 内部的某处使用 exit(),是否也保证调用该析构函数?

最佳答案

如果您调用 exit , 析构函数不会被调用。

来自 C++ 标准(§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.

关于c++ - exit() 或异常会阻止调用范围结束的析构函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2668075/

相关文章:

c++ - 强制自动并行化 VS 2012

c++ - 琐碎的可破坏性和调用析构函数的必要性

c++ - 对象的破坏导致程序崩溃

c++ - 无法退出 while 循环与 boolean 条件 c++

C++ 如何正确地将 const float 舍入为 unsigned int

c++ - 对 `pthread_cancel' 的 undefined reference

C++ std::getline 错误

c++ - 有人可以解释这个 C++ union 示例吗?

node.js - 如何停止执行 node.js 脚本?

react-native - react native : Double back press to Exit App