How safer to use boos::shared_from_this() from derived class inherited from based class?
Hi, I have a derived base class: class Base : boost::enable_shared_from_this<Base> { public: Base(); virtual ~Base(); virtual boost::shared_ptr<Base> SharedFromThis(void) {return shared_from_this();} private: } class Derived : public Base { public: Derived(); ~Derived(); // boost::shared_ptr<Derived> SharedFromThis(void) {return boost::static_pointer_cast<Derived>(shared_from_this());} private: }; Applications are sharing the derived class, it can either use shared_ptr<Derived> or shared_ptr<Base> by calling the SharedFromThis(), three options: (1) Don't define derived class own SharedFromThis(), always call Base SharedFromThis() to pass shared_ptr<Base> as Derived shared_ptr, the reference count will be shared_ptr<Base>. (2) Define derived class its own SharedFromThis() and pass shared_ptr<Derived> to applications: boost::shared_ptr<Derived> SharedFromThis(void) {return boost::static_pointer_cast<Derived>(shared_from_this());} That one cannot be virtual as conflicted to Base SharedFromThis(), the reference count will always be boost::shared_ptr<Derived> (3) Mixed both boost::shared_ptr<Derived> and boost::shared_ptr<Base> in applications, that could be a bad option to cause segmentation as one shared_ptr could be ended before another shared_ptr. Which option will be safer? Thank you. Kind regards, - jupiter
JH wrote:
Hi,
I have a derived base class:
class Base : boost::enable_shared_from_this<Base> { public: Base(); virtual ~Base(); virtual boost::shared_ptr<Base> SharedFromThis(void) {return shared_from_this();} private: }
class Derived : public Base { public: Derived(); ~Derived();
// boost::shared_ptr<Derived> SharedFromThis(void) {return boost::static_pointer_cast<Derived>(shared_from_this());} private:
};
Applications are sharing the derived class, it can either use shared_ptr<Derived> or shared_ptr<Base> by calling the SharedFromThis(), three options:
(1) Don't define derived class own SharedFromThis(), always call Base SharedFromThis() to pass shared_ptr<Base> as Derived shared_ptr, the reference count will be shared_ptr<Base>.
(2) Define derived class its own SharedFromThis() and pass shared_ptr<Derived> to applications:
boost::shared_ptr<Derived> SharedFromThis(void) {return boost::static_pointer_cast<Derived>(shared_from_this());}
That one cannot be virtual as conflicted to Base SharedFromThis(), the reference count will always be boost::shared_ptr<Derived>
(3) Mixed both boost::shared_ptr<Derived> and boost::shared_ptr<Base> in applications, that could be a bad option to cause segmentation as one shared_ptr could be ended before another shared_ptr.
Which option will be safer?
The shared_ptr<Derived> that is returned by the static_pointer_cast shares the same reference count as the original shared_ptr<Base>, so it shouldn't matter.
On 8/2/19, Peter Dimov via Boost
The shared_ptr<Derived> that is returned by the static_pointer_cast shares the same reference count as the original shared_ptr<Base>, so it shouldn't matter.
Good point, I missed out that part. That function cannot be virtual but I guess it does no matter, that should be safer to use. Thanks Peter. - jupiter
participants (2)
-
JH
-
Peter Dimov