码迷,mamicode.com
首页 > 其他好文 > 详细

智能指针weak_ptr记录

时间:2019-12-23 13:41:27      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:地方   因此   public   代码   mamicode   计数   other   输出   过期   

智能指针weak_ptr为弱共享指针,实际上是share_ptr的辅助指针,不具备指针的功能。主要是为了协助 shared_ptr 工作,可用来观测资源的使用情况。weak_ptr 只对 shared_ptr 进行引用,而不改变其引用计数,当被观察的 shared_ptr 失效后,相应的 weak_ptr 也相应失效。use_count() 可以观测资源的引用计数,lock()返回一个对象的可用的share_ptr,若weak_ptr.expired()为true过期,则返回一个空的share_ptr。

构造:

std::shared_ptr<int> sp(new int);
std::weak_ptr<int> wp1;
std::weak_ptr<int> wp2(wp1);
std::weak_ptr<int> wp3(sp);
std::cout << "use_count:\n";
std::cout << "wp1: " << wp1.use_count() << \n;
std::cout << "wp2: " << wp2.use_count() << \n;
std::cout << "wp3: " << wp3.use_count() << \n;

输出引用计数:

技术图片

 

过期判断:

std::shared_ptr<int> shared(new int(10));
std::weak_ptr<int> weak(shared);
std::cout << "1. weak " << (weak.expired() ? "is" : "is not") << " expired\n";
shared.reset();
std::cout << "2. weak " << (weak.expired() ? "is" : "is not") << " expired\n";

输出:

技术图片

 

lock()返回可用的share_ptr或空share_ptr

std::shared_ptr<int> sp1, sp2;
std::weak_ptr<int> wp;
sp1 = std::make_shared<int>(20); // sp1
wp = sp1;                        // sp1, wp
sp2 = wp.lock();                 // sp1, wp, sp2
sp1.reset();                     //      wp, sp2
sp1 = wp.lock();                 // sp1, wp, sp2
std::cout << "*sp1: " << *sp1 << \n;
std::cout << "*sp2: " << *sp2 << \n;

输出:

技术图片

 

#########################################################################################################

一个问题:shared_ptr是采用引用计数的智能指针,多个shared_ptr可以指向同一个动态对象,并共用了一个引用计数器。因此会出现循环引用的问题,导致计数器无法减一,因而内容在该销毁的地方没有释放。

eg:

class Brother;
class Sister
{
public:
    Sister() { cout << "Sister Constructor..." << endl; }
    ~Sister() { cout << "Sister Destructor..." << endl; }
    shared_ptr<Brother> _bro;
};
class Brother
{
public:
    Brother() { cout << "Brother Constructor..." << endl; }
    ~Brother() { cout << "Brother Destructor..." << endl; }
    shared_ptr<Sister> _sis;
};


shared_ptr<Sister> sps = make_shared<Sister>();
shared_ptr<Brother> spb = make_shared<Brother>();
sps->_bro = spb;
spb->_sis = sps;
std::cout << "sps use_cout:" << sps.use_count() << std::endl;
std::cout << "spb use_cout:" << spb.use_count() << std::endl;

输出:

技术图片

引用计数器为2,析构函数没有调用,出现程序执行完内容没有释放。

 

使用weak_ptr来处理这一问题,只需要将相互引用使用weak_ptr来替换share_ptr的引用即可

class Sister
{
public:
    Sister() { cout << "Sister Constructor..." << endl; }
    ~Sister() { cout << "Sister Destructor..." << endl; }
    weak_ptr<Brother> _bro;
};
class Brother
{
public:
    Brother() { cout << "Brother Constructor..." << endl; }
    ~Brother() { cout << "Brother Destructor..." << endl; }
    weak_ptr<Sister> _sis;
};

同样的输出代码:

技术图片

正常析构,程序执行完释放内容。

智能指针weak_ptr记录

标签:地方   因此   public   代码   mamicode   计数   other   输出   过期   

原文地址:https://www.cnblogs.com/tyche116/p/12082946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!