shared_ptr不能循环(嵌套)使用

在设计class时,该class可能会被用作智能指针,若其成员变量使用了指向自己的shared_ptr,这样就形成了shared_ptr的嵌套,导致资源无法回收,造成内存泄漏。
官方解释:
因为实现使用引用计数,所以shared_ptr实例的循环将不会被回收。例如,如果main()将shared_ptr保存到a,而后者直接或间接地将shared_ptr保存回a,则a的使用计数将为2。对原始的’ shared_ptr的破坏将会留下一个使用计数为1的悬空值。使用weak_ptr来“中断循环”。
Because the implementation uses reference counting, cycles of shared_ptr instances will not be reclaimed. For example, if main() holds a shared_ptr to A, which directly or indirectly holds a shared_ptr back to A, A’s use count will be 2. Destruction of the original `shared_ptr will leave A dangling with a use count of 1. Use weak_ptr to “break cycles.”

解决方案:
通过weak_ptr防止shared_ptr循环计数

test result:

#include "boost/smart_ptr.hpp"
#include <iostream>

using namespace boost;
using namespace std;

class B;
class A
{
public:
    A(){ cout << "A()\n";}
    ~A(){ cout << "~A()\n";}
    void setptr(boost::shared_ptr<B> b)
    {
        m_pb = b;
    }
    void setptrref(boost::shared_ptr<B> &b)
    {
        m_pb = b;
    }
    boost::shared_ptr<B> m_pb;
    boost::weak_ptr<B> m_wb;
};

class B
{
public:
    B(){ cout << "B()\n";}
    ~B(){ cout << "~B()\n";}
    boost::shared_ptr<A> m_pa;
    boost::weak_ptr<A> m_wa;
};

class C
{
public:
    C(){ cout << "c()\n";}
    ~C(){ cout << "~c()\n";}
    boost::shared_ptr<C> m_pc;
};

// 间接嵌套
void t1()
{
    boost::shared_ptr<A> pa = boost::make_shared<A>();
    boost::shared_ptr<B> pb = boost::make_shared<B>();
    pa->m_pb = pb;
    pb->m_pa = pa;
    std::cout << __FUNCTION__ << " pa count: " << pa.use_count() << std::endl;
    std::cout << __FUNCTION__ << " pb count: " << pb.use_count() << std::endl;
}
// copy construct
void t2()
{
    boost::shared_ptr<B> pb = boost::make_shared<B>();
    boost::shared_ptr<B> pb1 = pb;
    std::cout << __FUNCTION__ << " pb count: " << pb.use_count() << std::endl;
    std::cout << __FUNCTION__ << " pb1 count: " << pb1.use_count() << std::endl;
}
// assign
void t3()
{
    boost::shared_ptr<B> pb = boost::make_shared<B>();
    boost::shared_ptr<B> pb1;
    pb1 = pb;
    std::cout << __FUNCTION__ << " pb count: " << pb.use_count() << std::endl;
    std::cout << __FUNCTION__ << " pb1 count: " << pb1.use_count() << std::endl;
}
// 直接嵌套
void t4()
{
    boost::shared_ptr<C> pc = boost::make_shared<C>();
    pc->m_pc = pc;
    std::cout << __FUNCTION__ << " pc count: " << pc.use_count() << std::endl;
}
// weak_ptr
void t5()
{
    boost::shared_ptr<A> pa = boost::make_shared<A>();
    boost::shared_ptr<B> pb = boost::make_shared<B>();
    pa->m_wb = pb;
    pb->m_wa = pa;
    std::cout << __FUNCTION__ << " pa count: " << pa.use_count() << std::endl;
    std::cout << __FUNCTION__ << " pb count: " << pb.use_count() << std::endl;
}

void t6()
{
    boost::shared_ptr<A> pa = boost::make_shared<A>();
    boost::shared_ptr<B> pb = boost::make_shared<B>();
    pa->setptr(pb);
    std::cout << __FUNCTION__ << " pa count: " << pa.use_count() << std::endl;
    std::cout << __FUNCTION__ << " pb count: " << pb.use_count() << std::endl;
}

void t7()
{
    boost::shared_ptr<A> pa = boost::make_shared<A>();
    boost::shared_ptr<B> pb = boost::make_shared<B>();
    pa->setptrref(pb);
    std::cout << __FUNCTION__ << " pa count: " << pa.use_count() << std::endl;
    std::cout << __FUNCTION__ << " pb count: " << pb.use_count() << std::endl;
}

void t8()
{
    boost::shared_ptr<A> pa = boost::make_shared<A>();
    boost::shared_ptr<B> pb = boost::make_shared<B>();
    pa->m_pb = pb;
    std::cout << __FUNCTION__ << " pa count: " << pa.use_count() << std::endl;
    std::cout << __FUNCTION__ << " pb count: " << pb.use_count() << std::endl;
}

void testboostptr()
{
    t1();
    t2();
    t3();
    t4();
    t5();
    t6();
    t7();
    t8();
}

A()
B()
t1 pa count: 2
t1 pb count: 2
B()
t2 pb count: 2
t2 pb1 count: 2
~B()
B()
t3 pb count: 2
t3 pb1 count: 2
~B()
c()
t4 pc count: 2
A()
B()
t5 pa count: 1
t5 pb count: 1
~B()
~A()
A()
B()
t6 pa count: 1
t6 pb count: 2
~A()
~B()
A()
B()
t7 pa count: 1
t7 pb count: 2
~A()
~B()
A()
B()
t8 pa count: 1
t8 pb count: 2
~A()
~B()

从结果可以看出,t1()和t4()使用了shared_ptr嵌套,导致资源没有被回收。t5()中使用了weak_ptr,则资源能够正常回收。
总结归纳为两类问题:

  1. 自我嵌套,如t4()用例
  2. 相互依赖,你中有我我中有你,如t1()用例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章