Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如果构造函数在调用std::make_shared时崩溃,gdb能否显示崩溃的详细信息_C++_C++11_Gdb_Shared Ptr_Core - Fatal编程技术网

C++ 如果构造函数在调用std::make_shared时崩溃,gdb能否显示崩溃的详细信息

C++ 如果构造函数在调用std::make_shared时崩溃,gdb能否显示崩溃的详细信息,c++,c++11,gdb,shared-ptr,core,C++,C++11,Gdb,Shared Ptr,Core,在下面的代码中,我调用make_shared,MyClass的构造函数抛出异常。如果核心文件可用,是否有可能在gdb的帮助下找出崩溃的根源[例如:崩溃是来自foo()还是来自fun() #include <iostream> #include <memory> using namespace std; class MyClass { public: MyClass() { foo();

在下面的代码中,我调用
make_shared
,MyClass的构造函数抛出异常。如果核心文件可用,是否有可能在gdb的帮助下找出崩溃的根源[例如:崩溃是来自foo()还是来自fun()

#include <iostream>
#include <memory>
using namespace std;

class MyClass
{
    public:
        MyClass()
        {
            foo();
            fun();
        }

        ~MyClass() { }

        void foo()
        {
            throw("error 1");
        }
        void fun()
        {
            throw("error 2");
        }
};


shared_ptr<MyClass> createMyClass()
{
    return make_shared<MyClass>();
}

int main()
{
    shared_ptr<MyClass> c = createMyClass();
    return 0;
}
#包括
#包括
使用名称空间std;
类MyClass
{
公众:
MyClass()
{
foo();
乐趣();
}
~MyClass(){}
void foo()
{
抛出(“错误1”);
}
虚无乐趣()
{
抛出(“错误2”);
}
};
共享\u ptr createMyClass()
{
返回make_shared();
}
int main()
{
共享_ptr c=createMyClass();
返回0;
}
回溯仅指向此行:

 29     return make_shared<MyClass>();
29返回make_shared();
回溯:

Program received signal SIGABRT, Aborted.
0x00007ffff722d5f7 in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.17-106.el7_2.6.x86_64 libgcc-4.8.5-4.el7.x86_64 libstdc++-4.8.5-4.el7.x86_64

    (gdb) bt
    #0  0x00007ffff722d5f7 in raise () from /lib64/libc.so.6
    #1  0x00007ffff722ece8 in abort () from /lib64/libc.so.6
    #2  0x00007ffff7b329d5 in __gnu_cxx::__verbose_terminate_handler() () from /lib64/libstdc++.so.6
    #3  0x00007ffff7b30946 in ?? () from /lib64/libstdc++.so.6
    #4  0x00007ffff7b30973 in std::terminate() () from /lib64/libstdc++.so.6
    #5  0x00007ffff7b30be9 in __cxa_rethrow () from /lib64/libstdc++.so.6
    #6  0x000000000040121e in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<MyClass, std::allocator<MyClass>>(std::_Sp_make_shared_tag, MyClass*, std::allocator<MyClass> const&) (this=0x7fffffffe178, __a=...) at /usr/include/c++/4.8.2/bits/shared_ptr_base.h:509
    #7  0x00000000004010ba in std::__shared_ptr<MyClass, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<MyClass>>(std::_Sp_make_shared_tag, std::allocator<MyClass> const&) (this=0x7fffffffe170, __tag=..., __a=...) at /usr/include/c++/4.8.2/bits/shared_ptr_base.h:957
    #8  0x0000000000401052 in std::shared_ptr<MyClass>::shared_ptr<std::allocator<MyClass>>(std::_Sp_make_shared_tag, std::allocator<MyClass> const&) (this=0x7fffffffe170,
        __tag=..., __a=...) at /usr/include/c++/4.8.2/bits/shared_ptr.h:316
    #9  0x0000000000400f98 in std::allocate_shared<MyClass, std::allocator<MyClass>>(std::allocator<MyClass> const&) (__a=...) at /usr/include/c++/4.8.2/bits/shared_ptr.h:598
    #10 0x0000000000400ee0 in std::make_shared<MyClass<> > () at /usr/include/c++/4.8.2/bits/shared_ptr.h:614
    #11 0x0000000000400ce3 in createMyClass () at abrt.cpp:29
    #12 0x0000000000400cfe in main () at abrt.cpp:34
    (gdb) q
程序收到信号SIGABRT,中止。
/lib64/libc.so.6中的提升()中的0x00007FF722D5F7
缺少单独的调试信息,请使用:debuginfo安装glibc-2.17-106.el7_2.6.x86_64 libgcc-4.8.5-4.el7.x86_64 libstdc++-4.8.5-4.el7.x86_64
(gdb)英国电信
#在/lib64/libc.so.6的raise()中0 0x00007FF722D5F7
#1 0x00007FF722ECE8位于/lib64/libc.so.6的中止()中
#2 0x00007FF7B329D5,位于/lib64/libstdc++.so.6中的uu gnu_cxx::u verbose_terminate_handler()
#3 0x00007FF7B30946英寸??()来自/lib64/libstdc++.so.6
#来自/lib64/libstdc++.so.6的std::terminate()中的4 0x00007ffff7b30973
#5 0x00007FF7B30BE9在/lib64/libstdc++.so.6的
#在/usr/include/c++/4.8.2/bits/shared\ptr\u base.h:509中,std:::::::::::::::::::::::::::::(std::)make\u shared\u标记,MyClass*,std::allocator const&)(this=0x7fffffffffe178,_ua=…)
#在/usr/include/c++/4.8.2/bits/shared\ptr base.h:957中,std:::::::::::::::::::::
#std::shared_ptr::shared_ptr(std:_Sp_make_shared_标记,std::allocator const&)中的8 0x0000000000401052(此参数=0x7fffffffe170,
__在/usr/include/c++/4.8.2/bits/shared_ptr.h:316处标记=…,u a=…)
#9 0x0000000000400f98位于/usr/include/c++/4.8.2/bits/shared\u ptr.h:598的std::allocate\u shared(std::allocator const&)(\uu a=…)中
#在/usr/include/c++/4.8.2/bits/shared_ptr.h:614的std::make_shared()中有10 0x0000000000400ee0
#abrt.cpp:29处createMyClass()中的11 0x0000000000400ce3
#在abrt.cpp:34处的主管道()中有12个0x0000000000400cfe
(gdb)q

生成核心文件时,信息丢失。原因是
共享\u ptr
构造函数必须捕获对象构造函数中的任何异常,以便能够取消分配它先前分配的内存(以防止
抛出的构造函数导致的泄漏)。当它捕获异常以释放内存时,它不再知道异常在构造函数中的何处抛出。

在生成核心文件时,该信息将丢失。原因是
共享\u ptr
构造函数必须捕获对象构造函数中的任何异常,以便能够取消分配它先前分配的内存(以防止
抛出的构造函数导致的泄漏)。当它捕获异常以释放内存时,它不再知道异常在构造函数中的何处抛出。

从回溯中获取结果的最佳方法是在关闭所有优化的情况下进行编译。否则,编译器可能会选择内联函数,而回溯将丢失内容。在没有任何优化标志的情况下重新编译,看看是否能从中获得更好的回溯。请演示如何生成此代码。@SamVarshavchik,我尝试了-O0,结果仍然相同。@ks1322,我编译为“g++-ggdb-O0-std=c++11 abrt.cpp”。从回溯中获得结果的最佳方法是在关闭所有优化的情况下进行编译。否则,编译器可能会选择内联函数,而回溯将丢失内容。在没有任何优化标志的情况下重新编译,看看是否能从中获得更好的回溯。请说明如何生成此代码。@SamVarshavchik,我用-O0进行了尝试,结果仍然相同。@ks1322,我编译为“g++-ggdb-O0-std=c++11 abrt.cpp”