如果构造函数从调用到 std::make_shared 崩溃,GDB 是否可以显示崩溃的详细信息
if constructor crashes from call to std::make_shared, can gdb show details of crash
在下面的代码中,我调用make_shared<MyClass>
,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;
}
回溯仅指向以下行:
29 return make_shared<MyClass>();
回溯:
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
生成核心文件时,该信息将丢失。原因是shared_ptr
构造函数必须捕获对象构造函数中的任何异常,以便能够释放它之前分配的内存(以防止throw
的构造函数引起的泄漏(。当它捕获异常以释放内存时,它不再知道异常在构造函数中的位置被抛出。
相关文章:
- 包含未定义行为的源代码使编译器崩溃是否合法?
- 是否有可能存在不会崩溃程序的内存问题
- 是否可以以编程方式创建迷你转储文件而不会崩溃?
- 如果构造函数从调用到 std::make_shared 崩溃,GDB 是否可以显示崩溃的详细信息
- Windows C++程序中的访问冲突是否总是立即崩溃
- 故障转储文件是否需要崩溃时正在运行的本地文件
- 是否可以在程序崩溃后执行代码?
- 是否有支持C++和C#混合应用程序的崩溃报告服务
- 对 NULL 值的按位操作是否会使C++中的代码崩溃
- 确定程序是否崩溃
- C++程序在使用 if 条件检查指针是否为 NULL 时崩溃
- 是否有任何清单可以避免在使用 OpenCV SVM 时崩溃
- 如果测试程序崩溃,Valgrind报告是否可信
- 正在检查程序是否崩溃
- 如果机器立即崩溃,那么fflush()在将数据保存到磁盘方面是否与fclose()一样好?
- 如何知道当程序在运行时崩溃时是否是内存错误"the program has stopped working"
- 一种检测进程是否崩溃(或停止)的简单方法
- 我的win32程序是否有办法告诉它启动的子进程已经崩溃(而不仅仅是退出)?
- c++程序在崩溃时是否自动释放内存?
- 用于定义一个数字是否为素数的函数会导致c++程序崩溃