使用boost :: shared_ptr的时候有什么潜在的危险? [英] What are potential dangers when using boost::shared_ptr?

查看:90
本文介绍了使用boost :: shared_ptr的时候有什么潜在的危险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一些你可以使用<一拍的时候自己的脚的方式href=\"http://www.boost.org/doc/libs/release/libs/smart_ptr/shared_ptr.htm\"><$c$c>boost::shared_ptr?换句话说,我有什么陷阱,以避免当我使用<一个href=\"http://www.boost.org/doc/libs/release/libs/smart_ptr/shared_ptr.htm\"><$c$c>boost::shared_ptr?

What are some ways you can shoot yourself in the foot when using boost::shared_ptr? In other words, what pitfalls do I have to avoid when I use boost::shared_ptr?

推荐答案

循环引用:一个的shared_ptr&LT;&GT; 来的东西,有一个的shared_ptr&LT ;&GT; 原来的对象。您可以使用的weak_ptr&LT;方式&gt; 来打破这种恶性循环,当然

Cyclic references: a shared_ptr<> to something that has a shared_ptr<> to the original object. You can use weak_ptr<> to break this cycle, of course.

我添加下面的什么,我在评论中谈到的一个例子。

I add the following as an example of what I am talking about in the comments.

class node : public enable_shared_from_this<node> {
public :
    void set_parent(shared_ptr<node> parent) { parent_ = parent; }
    void add_child(shared_ptr<node> child) {
        children_.push_back(child);
        child->set_parent(shared_from_this());
    }

    void frob() {
        do_frob();
        if (parent_) parent_->frob();
    }

private :
    void do_frob();
    shared_ptr<node> parent_;
    vector< shared_ptr<node> > children_;
};

在此例中,将具有节点树,其中每一个容纳一个指针到它的父类。该FROB()成员函数,无论出于何种原因,涟漪向上通过树。 (这不完全是古怪的;一些GUI框架,以这种方式工作)。

In this example, you have a tree of nodes, each of which holds a pointer to its parent. The frob() member function, for whatever reason, ripples upwards through the tree. (This is not entirely outlandish; some GUI frameworks work this way).

问题是,如果你失去了参照最上面的节点,然后在最上面的节点仍持有其子强引用,其所有的孩子还抱着一个强引用他们的父母。这意味着,有循环引用将所有的实例从清洁而上,虽然没有实际从code,此内存泄漏。

The problem is that, if you lose reference to the topmost node, then the topmost node still holds strong references to its children, and all its children also hold a strong reference to their parents. This means that there are circular references keeping all the instances from cleaning themselves up, while there is no way of actually reaching the tree from the code, this memory leaks.

class node : public enable_shared_from_this<node> {
public :
    void set_parent(shared_ptr<node> parent) { parent_ = parent; }
    void add_child(shared_ptr<node> child) {
        children_.push_back(child);
        child->set_parent(shared_from_this());
    }

    void frob() {
        do_frob();
        shared_ptr<node> parent = parent_.lock(); // Note: parent_.lock()
        if (parent) parent->frob();
    }

private :
    void do_frob();
    weak_ptr<node> parent_; // Note: now a weak_ptr<>
    vector< shared_ptr<node> > children_;
};

在这里,父节点已经被替换为弱指针。它不再具有它所引用节点的寿命有发言权。因此,如果最顶层的节点超出范围作为在previous例子,那么同时它持有其子强引用,它的孩子不抱到自己的父母强引用。因此,有对象没有强引用,并清除自己的身体。反过来,这会导致孩子失去其一端有力的参考,这将导致它们进行清理,等等。总之,这不会泄漏。而刚刚通过战略性更换一个shared_ptr&LT;用的weak_ptr&LT>;>

Here, the parent node has been replaced by a weak pointer. It no longer has a say in the lifetime of the node to which it refers. Thus, if the topmost node goes out of scope as in the previous example, then while it holds strong references to its children, its children don't hold strong references to their parents. Thus there are no strong references to the object, and it cleans itself up. In turn, this causes the children to lose their one strong reference, which causes them to clean up, and so on. In short, this wont leak. And just by strategically replacing a shared_ptr<> with a weak_ptr<>.

这篇关于使用boost :: shared_ptr的时候有什么潜在的危险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆