I am currently building an application that relies heavily on shared_ptr , and everything looks good so far - I did my homework and you have a good idea about some problems using shared_ptr s.
One of the most known problems with shared_ptr is circular dependency - these problems can be solved by storing weak_ptr , which do not affect the lifetime of objects in the chain. However, I struggle to get my head when I need to store a pointer to an external object using weak_ptr - . I am not sure if it is forbidden, not recommended or safe .
The following diagram describes what I mean (black arrows indicate shared_ptr , dotted lines indicate weak_ptr ):
alt text http://img694.imageshack.us/img694/6628/sharedweakptr.png
- The parent contains
shared_ptr for two children, both of which access the parent using weak_ptr . - In the constructor of the first child, I get through the parent
weak_ptr pointer to the second child element and save it locally.
The code is as follows:
#include <boost/shared_ptr.hpp> #include <boost/weak_ptr.hpp> #include <boost/make_shared.hpp> #include <boost/enable_shared_from_this.hpp> class child; class child2; class parent; class parent : public boost::enable_shared_from_this<parent> { public: void createChildren() { _child2 = boost::make_shared<child2>(shared_from_this()); _child = boost::make_shared<child>(shared_from_this()); } boost::shared_ptr<child> _child; boost::shared_ptr<child2> _child2; }; class child { public: child(boost::weak_ptr<parent> p) { _parent = p; _child2 = boost::shared_ptr<parent>(p)->_child2; // is this safe? } boost::weak_ptr<parent> _parent; boost::shared_ptr<child2> _child2; }; class child2 { public: child2(boost::weak_ptr<parent> p) { this->_parent = p; } boost::weak_ptr<parent> _parent; }; int main() { boost::shared_ptr<parent> master(boost::make_shared<parent>()); master->createChildren(); }
I tested this and it seems to be working fine (I have no memory leak messages), however my question is: Is it safe? And if not, why not?
c ++ boost shared-ptr weak-ptr
Alan
source share