简体   繁体   English

为什么在我的析构函数中没有调用 std::terminate

[英]why is std::terminate not called when throwing in my destructor

I'm trying to "see" the call to std::terminate() when throwing from a destructor with the following code:当使用以下代码从析构函数抛出时,我试图“查看”对 std::terminate() 的调用:

#include <stdexcept>

struct boom {
     ~boom() {
        throw std::logic_error("something went wrong");
     }
};

int main() {
     boom();
}

compiled with g++ and run the code:用 g++ 编译并运行代码:

# ./a.out
terminate called after throwing an instance of 'std::logic_error'
  what():  something went wrong
Aborted (core dumped)

so far so good, seems to work as expected (call terminate()).到目前为止一切顺利,似乎按预期工作(调用终止())。 But when trying to break on terminate in gdb, the function does not get hit and the backtrace shows only abort:但是当试图在 gdb 中终止时,function 没有被命中,并且回溯只显示中止:

(gdb) b std::terminate
Breakpoint 2 at 0x7f60a3772240 (2 locations)
(gdb) r
Starting program: a.out
terminate called after throwing an instance of 'std::logic_error'
  what():  something went wrong

Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007f2c241eb921 in __GI_abort () at abort.c:79
#2  0x00007f2c24628957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007f2c2462eae6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007f2c2462db49 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007f2c2462e4b8 in __gxx_personality_v0 () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007f2c23c05573 in ?? () from /lib/x86_64-linux-gnu/libgcc_s.so.1
#7  0x00007f2c23c05ad1 in _Unwind_RaiseException () from /lib/x86_64-linux-gnu/libgcc_s.so.1
#8  0x00007f2c2462ed47 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#9  0x0000558cee09593a in boom::~boom() ()
#10 0x0000558cee0958dd in main ()
(gdb)

This behavior is puzzling me somehow, is there something I am missing?这种行为不知何故让我感到困惑,我错过了什么吗?

EDIT:编辑:

same test using clang++ and the std::terminate breakpoint is hit:使用 clang++ 和 std::terminate 断点进行相同的测试:

Breakpoint 1, 0x0000000000400750 in std::terminate()@plt ()
(gdb) bt
#0  0x0000000000400750 in std::terminate()@plt ()
#1  0x00000000004009bf in __clang_call_terminate ()
#2  0x000000000187eef0 in ?? ()
#3  0x00000000004009a4 in boom::~boom() ()
#4  0x00000000004008f1 in main ()
(gdb)

You are running into the as-if rule.您遇到了 as-if 规则。

C++ compilers are allowed to essentially rewrite your entire program as they see fit as long as the side effects remain the same.只要副作用保持不变,C++ 编译器就可以基本上重写您的整个程序,只要它们认为合适。

What a side effect is very well defined, and "a certain function gets called" is not part of that.副作用的定义非常明确,并且“某个 function 被调用”不是其中的一部分。 So the compiler is perfectly allowed to just call abort() directly if it can determine that this is the only side effect std::terminate() would have on the program.因此,如果编译器可以确定这是std::terminate()对程序产生的唯一副作用,则完全允许编译器直接调用abort()

You can get more details at https://en.cppreference.com/w/cpp/language/as_if您可以在https://en.cppreference.com/w/cpp/language/as_if获得更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我总是在抛出我的析构函数时“抛出一个……的实例后调用终止”? - Why do I always get “terminate called after throwing an instance of…” when throwing in my destructor? 调用析构函数时清空std :: list抛出异常 - empty std::list throwing exception when destructor is called 为什么在抛出'std :: bad_alloc'的实例后终止调用? - why terminate called after throwing an instance of 'std::bad_alloc'? 为什么我的程序在析构函数抛出异常时终止? - Why does my program terminate when an exception is thrown by a destructor? 抛出std :: string实例后终止调用 - Terminate called after throwing an instance of std::string 从防护类析构函数抛出异常会导致std :: terminate - Throwing exception from guard class destructor causes std::terminate 抛出std :: exception实例后终止调用 - Terminate called after throwing an instance of std::exception 在抛出 'std::string' 的实例后调用终止 - terminate called after throwing an instance of 'std::string' 何时调用std :: thread析构函数? - When is std::thread destructor called? 在为我的程序抛出一个 &#39;std::out_of_range&#39; 实例后调用 Terminate 来查找和替换字符串中的单词 - Terminate called after throwing an instance of 'std::out_of_range' for my program which finds and replaces words in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM