我試圖追捕爲什麼一個程序崩潰在shared_ptr
。爲什麼我的程序在boost :: enable_shared_from_this <>/boost :: 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> ([email protected]) at throw_exception.hpp:66
#10 0x0000000100057bf4 in boost::detail::shared_count::shared_count (this=0x1002c5d00, [email protected]) at shared_count.hpp:509
#11 0x0000000100057b7d in boost::detail::shared_count::shared_count (this=0x1002c5d00, [email protected]) at shared_count.hpp:511
#12 0x000000010004ad14 in boost::shared_ptr<myns::(anonymous namespace)::MySharedFromThisClass>::shared_ptr<myns::(anonymous namespace)::MySharedFromThisClass> (this=0x1002c5cf8, [email protected]) at shared_ptr.hpp:220
#13 0x000000010004acad in boost::shared_ptr<myns::(anonymous namespace)::MySharedFromThisClass>::shared_ptr<myns::(anonymous namespace)::MySharedFromThisClass> (this=0x1002c5cf8, [email protected]) 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());
}
什麼原因呢?沒有任何警告或錯誤的東西編譯,但事情非常清楚地以不愉快的方式進行段錯誤。
ABI在這裏不是問題。正如預期的那樣,您只會得到'bad_weak_ptr'異常,但不要捕獲它,所以線程(和進程)終止。 – 2013-04-10 07:56:42
使用'enable_shared_from_this <>'被調用'shared_form_this()'這個異常的對象的構造函數的一個常見原因被共享 - 不是一種罕見的事情,如果你想傳遞一個指向對象到其聚集的構造成員。整潔的解決辦法是宣佈你的構造私人和提供工廠方法,在其中您可以用'shared_form_this()'同性戀放棄。 – marko 2013-04-10 14:18:15