为什么我的程序在 boost::enable_shared_from_this<>/boost::shared_ptr<> 中崩溃?
Why is my program crashing in boost::enable_shared_from_this<>/boost::shared_ptr<>?
我试图找出程序在shared_ptr
中崩溃的原因。
#0 0x00007fff90723212 in __pthread_kill ()
#1 0x00007fff93415b54 in pthread_kill ()
#2 0x00007fff93459dce in abort ()
#3 0x00007fff8a0519eb in abort_message ()
#4 0x00007fff8a04f39a in default_terminate ()
#5 0x00007fff930bf887 in _objc_terminate ()
#6 0x00007fff8a04f3c9 in safe_handler_caller ()
#7 0x00007fff8a04f424 in std::terminate ()
#8 0x00007fff8a05058b in __cxa_throw ()
#9 0x0000000100057cbc in boost::throw_exception<boost::bad_weak_ptr> (e=@0x1002c5b88) at throw_exception.hpp:66
#10 0x0000000100057bf4 in boost::detail::shared_count::shared_count (this=0x1002c5d00, r=@0x100304180) at shared_count.hpp:509
#11 0x0000000100057b7d in boost::detail::shared_count::shared_count (this=0x1002c5d00, r=@0x100304180) at shared_count.hpp:511
#12 0x000000010004ad14 in boost::shared_ptr<myns::(anonymous namespace)::MySharedFromThisClass>::shared_ptr<myns::(anonymous namespace)::MySharedFromThisClass> (this=0x1002c5cf8, r=@0x100304178) at shared_ptr.hpp:220
#13 0x000000010004acad in boost::shared_ptr<myns::(anonymous namespace)::MySharedFromThisClass>::shared_ptr<myns::(anonymous namespace)::MySharedFromThisClass> (this=0x1002c5cf8, r=@0x100304178) at shared_ptr.hpp:223
#14 0x000000010004a9b4 in boost::enable_shared_from_this<myns::(anonymous namespace)::MySharedFromThisClass>::shared_from_this (this=0x100304178) at enable_shared_from_this.hpp:49
MySharedFromThisClass定义为:
class MySharedFromThis : public boost::enable_shared_from_this<MySharedFromThis> {
// ....
};
传递的实例被定义为:
auto myKlass = std::make_shared<MySharedFromThis>();
并通过进行复制
void myFunction(::boost::shared_ptr<MySharedFromThis> myKlass) {
myFunction(shared_from_this());
}
什么东西?所有的编译都没有任何警告或错误,但事情显然以不那么愉快的方式发生了故障。
我发布了我自己的答案,因为这段代码编译时没有任何警告或错误,并且花了相当多的精力来追踪。
基于上面的例子,当看到这条线时,它应该是非常明显的:
auto myKlass = std::make_shared<MySharedFromThis>();
myKlass的类型是std::shared_ptr<MySharedFromThis>
,而不是boost::shared_ptr<MySharedFromThis>
。将std::make_shared<>
更改为boost::make_shared<>
,一切都按预期/预期进行。
有两种可能导致这次崩溃:
- 调用myFunction((中对
shared_from_this()
的调用时没有现有的::boost::shared_ptr<>
。相反,所发生的是创建了一个std::shared_ptr<>
,它具有与boost::shared_ptr<>
不同的ABI。值得注意的是,libboost_thread-mt.dylib
期望boost::enable_shared_from_this<>
类而不是std::enable_shared_from_this<>
类 根据enable_shared_from_this<>文件,前提条件不满足。
要求:enable_shared_from_this必须是T的可访问基类。*它必须是T类型实例T的子对象。必须至少存在一个拥有t.的shared_ptr实例p
相反,出现了一个
std::shared_ptr<>
实例,但没有boost::shared_ptr<>
实例。
至少这是我对发生的事情的理解。我认为#1是导致segfault的真正问题,#2迟早会成为问题(尽管我并不完全相信(。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- Boost Spirit,获取迭代器内部语义动作
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 链接 boost 库时"Error while loading shared libraries"引发的,除了我无法使用 root 访问权限来修复它
- boost::shared_ptr和std::shared-ptr的同居
- 将GoogleMock与Boost::Shared Pointers一起使用时泄漏的Mock对象
- 我可以用std::shared_ptr而不是boost::shared-ptr构建boost库吗
- QSharedPointer VS std::tr1::shared_ptr VS boost::tr1::shared