C++,捕获错误作为引用时的隔离错误

C++, Segfault when catching error as refernce

本文关键字:错误 引用 隔离 C++      更新时间:2023-10-16

当我捕获任何异常作为引用并尝试使用 e.what() 访问它时,我得到一个 Segfault。有没有人知道,为什么会这样?

#include <stdexcept>
#include <iostream>
int main(){
    try{
        throw ::std::runtime_error("test");
    }catch(::std::exception const &e){
        ::std::cerr << e.what() << "n";
    }
}

我得到:

 bash: line 1: 22110 Segmentation fault      (core dumped) 'test' < /dev/null > 'test.stdout' 2> 'test.stderr'

使用ASAN,我得到:

ASAN:SIGSEGV
=================================================================
==21318==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x2af35c5534bf bp 0x7ffc1b30ee30 sp 0x7ffc1b30ee10 T0)
    #0 0x2af35c5534be  (/media/psf/Home/test+0xf54be)
    #1 0x2af35ebf4ec4  (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4)
    #2 0x2af35c4a68a6  (/media/psf/Home/test+0x488a6)
AddressSanitizer can not provide additional info.
==21318==ABORTING

我使用: Ubuntu clang 版本 3.6.0-2ubuntu1~trusty1 (tags/RELEASE_360/final) (基于 LLVM 3.6.0)

上: Ubuntu

14.04.3 LTS,Linux ubuntu 3.13.0-76-generic #120-Ubuntu SMP 周一 1月 18 15:59:10 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

解决方案:删除 -lc++abi 为我解决了问题。

解决方案:删除编译器标志 -lc++abi 为我解决了这个问题。