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()用例
【为什么还需要学习C++?】 你是否接触很多语言,但从来没有了解过编程语言的本质?你是否想成为一名资深开发人员,想开发别人做不了的高性能程序?你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹? 那么C++就是你个人能力提升,职业之路进阶的不二之选。【课程特色】 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。【学完后我将达到什么水平?】 1.对C++的各个知识能够熟练配置、开发、部署;2.吊打一切关于C++的笔试面试题;3.面向物联网的“嵌入式”和面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。【面向人群】 1.希望一站式快速入门的C++初学者; 2.希望快速学习 C++、掌握编程要义、修炼内功的开发者; 3.有志于挑战更高级的开发项目,成为资深开发的工程师。 【课程设计】 本课程包含3大模块基础篇本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。进阶篇本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接和命名空间等。提升篇:本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值