假设我有以下代码:
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/