我目前正在整理一个严重依赖的应用程序,shared_ptr
到目前为止一切看起来都很好——我已经完成了我的功课shared_ptr
,并且对使用s的一些陷阱有一个很好的了解。
最公认的问题之一shared_ptr
是循环依赖——这些问题可以通过存储weak_ptr
不影响链上对象生命周期的 s 来解决。但是,我正在努力解决需要通过 a 存储指向外部对象的指针的时间weak_ptr
-我不确定它是否被禁止、不鼓励或是否安全。
下图描述了我的意思(黑色箭头表示shared_ptr
;虚线表示weak_ptr
):
替代文字 http://img694.imageshack.us/img694/6628/sharedweakptr.png
- 父级包含
shared_ptr
s 到两个子级,这两个子级都使用 a 指向父级weak_ptr
。 - 在第一个孩子的构造函数中,我通过父级检索
weak_ptr
指向第二个孩子的指针并将其存储在本地。
代码如下所示:
#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();
}
我已经对此进行了测试,它似乎工作正常(我没有收到任何内存泄漏的报告),但是我的问题是:这安全吗?如果没有,为什么不呢?