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()用例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章