Bad_weak_ptr when calling shared_from_this () in the base class

I have a SuperParent class, a Parent class (derived from SuperParent ) and both contain the shared_ptr to Child class (which contains weak_ptr to SuperParent ). Unfortunately, I get a bad_weak_ptr exception when trying to set the Child pointer. The code is as follows:

 #include <boost/enable_shared_from_this.hpp> #include <boost/make_shared.hpp> #include <boost/shared_ptr.hpp> #include <boost/weak_ptr.hpp> using namespace boost; class SuperParent; class Child { public: void SetParent(shared_ptr<SuperParent> parent) { parent_ = parent; } private: weak_ptr<SuperParent> parent_; }; class SuperParent : public enable_shared_from_this<SuperParent> { protected: void InformChild(shared_ptr<Child> grandson) { grandson->SetParent(shared_from_this()); grandson_ = grandson; } private: shared_ptr<Child> grandson_; }; class Parent : public SuperParent, public enable_shared_from_this<Parent> { public: void Init() { child_ = make_shared<Child>(); InformChild(child_); } private: shared_ptr<Child> child_; }; int main() { shared_ptr<Parent> parent = make_shared<Parent>(); parent->Init(); return 0; } 
+4
source share
1 answer

This is because your Parent class inherits enable_shared_from_this twice. Instead, you should inherit it once - through SuperParent. And if you want to be able to get shared_ptr <Parent> in the parent class, you can also inherit it from the following helper class:

 template<class Derived> class enable_shared_from_This { public: typedef boost::shared_ptr<Derived> Ptr; Ptr shared_from_This() { return boost::static_pointer_cast<Derived>(static_cast<Derived *>(this)->shared_from_this()); } Ptr shared_from_This() const { return boost::static_pointer_cast<Derived>(static_cast<Derived *>(this)->shared_from_this()); } }; 

Then

 class Parent : public SuperParent, public enable_shared_from_This<Parent> 
+5
source

Source: https://habr.com/ru/post/1397574/


All Articles