c++ 如果构造函数在调用std::make_shared时崩溃,gdb可以显示崩溃的详细信息吗?

r1zk6ea1  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(220)

在下面的代码中,我正在调用make_shared<MyClass>,MyClass的构造函数抛出了一个异常。如果核心文件可用,是否可以在gdb的帮助下找到crash的起源[例如:crash是来自foo()还是fun()]?

  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4. class MyClass
  5. {
  6. public:
  7. MyClass()
  8. {
  9. foo();
  10. fun();
  11. }
  12. ~MyClass() { }
  13. void foo()
  14. {
  15. throw("error 1");
  16. }
  17. void fun()
  18. {
  19. throw("error 2");
  20. }
  21. };
  22. shared_ptr<MyClass> createMyClass()
  23. {
  24. return make_shared<MyClass>();
  25. }
  26. int main()
  27. {
  28. shared_ptr<MyClass> c = createMyClass();
  29. return 0;
  30. }

字符串
backtrace只指向这一行:

  1. 29 return make_shared<MyClass>();


回溯:

  1. Program received signal SIGABRT, Aborted.
  2. 0x00007ffff722d5f7 in raise () from /lib64/libc.so.6
  3. 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
  4. (gdb) bt
  5. #0 0x00007ffff722d5f7 in raise () from /lib64/libc.so.6
  6. #1 0x00007ffff722ece8 in abort () from /lib64/libc.so.6
  7. #2 0x00007ffff7b329d5 in __gnu_cxx::__verbose_terminate_handler() () from /lib64/libstdc++.so.6
  8. #3 0x00007ffff7b30946 in ?? () from /lib64/libstdc++.so.6
  9. #4 0x00007ffff7b30973 in std::terminate() () from /lib64/libstdc++.so.6
  10. #5 0x00007ffff7b30be9 in __cxa_rethrow () from /lib64/libstdc++.so.6
  11. #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
  12. #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
  13. #8 0x0000000000401052 in std::shared_ptr<MyClass>::shared_ptr<std::allocator<MyClass>>(std::_Sp_make_shared_tag, std::allocator<MyClass> const&) (this=0x7fffffffe170,
  14. __tag=..., __a=...) at /usr/include/c++/4.8.2/bits/shared_ptr.h:316
  15. #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
  16. #10 0x0000000000400ee0 in std::make_shared<MyClass<> > () at /usr/include/c++/4.8.2/bits/shared_ptr.h:614
  17. #11 0x0000000000400ce3 in createMyClass () at abrt.cpp:29
  18. #12 0x0000000000400cfe in main () at abrt.cpp:34
  19. (gdb) q

yyyllmsg

yyyllmsg1#

在生成核心文件时,这些信息会丢失,原因是shared_ptr构造函数必须捕获对象构造函数的任何异常,以便能够释放它先前分配的内存(防止throw的施工人员引起泄漏)当它捕获异常以释放内存时,它不再知道异常在构造函数中的何处被抛出。

相关问题