智能指针weak_ptr记录
智能指针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());
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>(); // 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记录的更多相关文章
- c/c++ 智能指针 weak_ptr 使用
智能指针 weak_ptr 使用 weak_ptr用途: 1,解决空悬指针问题 2,解决循环引用问题 weak_ptr特点:没有*操作和->操作 weak_ptr是不控制所指对象生存周期的智能指 ...
- C++智能指针--weak_ptr
weak_ptr是对对象的一种弱引用,它不会添加对象的引用计数.weak_ptr和shared_ptr之间能够相互转换.shared_ptr能够直接赋值给week_ptr,week_ptr可通过调用l ...
- Boost智能指针——weak_ptr
循环引用: 引用计数是一种便利的内存管理机制,但它有一个很大的缺点,那就是不能管理循环引用的对象.一个简单的例子如下: #include <string>#include <iost ...
- 智能指针share_ptr记录
shared_ptr 是一个共享所有权的智能指针,允许多个指针指向同一个对象.shared_ptr 对象除了包括一个对象的指针,还包括一个引用计数器.当每给对象分配一个share_ptr的时候,引用计 ...
- 智能指针weak_ptr解决循环依赖问题
#include <iostream> #include <memory> class Woman; class Man{ private: std::weak_ptr< ...
- 智能指针unique_ptr记录
unique_ptr 对对象独有管理,无法复制,共享,值传递,可以使用move语义来转移控制权. std::default_delete<int> d; std::unique_ptr&l ...
- [6] 智能指针boost::weak_ptr
[1]boost::weak_ptr简介 boost::weak_ptr属于boost库,定义在namespace boost中,包含头文件 #include<boost/weak_ptr.hp ...
- weak_ptr<T>智能指针
weak_ptr是为配合shared_ptr而引入的一种智能指针,它更像是shared_ptr的一个助手,而不是智能指针,因为它不具有普通指针的行为,没有重载operator*和operator-&g ...
- 智能指针shared_ptr新特性shared_from_this及weak_ptr
enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为: template< class T > class enable_shar ...
随机推荐
- Django使用DataTables插件总结
Django使用Datatables插件总结 文章中的例子已上传至github 基本使用 Datatables插件是一款方便简单的展示数据的列表插件.关于基本使用,官方网站上的已介绍的很详细,这里我再 ...
- Mybatis 批量操作以及多参数操作遇到的坑
查考地址:https://blog.csdn.net/shengtianbanzi_/article/details/80147134 待整理中......
- java网络编程-面试题
1.网络编程时的同步.异步.阻塞.非阻塞? 同步:函数调用在没得到结果之前,没有调用结果,不返回任何结果.异步:函数调用在没得到结果之前,没有调用结果,返回状态信息.阻塞:函数调用在没得到结果之前,当 ...
- hdu4813 01背包+前缀和
题意:\(A,B\)两人,有\(N\)个事件,每件发生的概率都为\(0.5\),若事件\(i\)发生,则\(B\)加\(v_i\)分数,若其不发生,则\(B\)不加分,给定一个概率\(P\),问至少需 ...
- golang数据基本数据类型和string类型的转换
基本类型之间的转换 golang在不同类型的变量之间赋值时需要显式转换,也就是说golang中数据类型不能自动转换. 表达式T(v)将值v转换为类型T 1.数据类型的转换可以是从范围小——>范围 ...
- Faster RCNN学习笔记
感谢知乎大神的分享 https://zhuanlan.zhihu.com/p/31426458 Ross B. Girshick在2016年提出了新的Faster RCNN,在结构上,Faster R ...
- c# 爬虫和组件HtmlAgilityPack处理html
测试当前爬虫的User-Agent:http://www.whatismyuseragent.net/ 大佬的博客地址:https://www.cnblogs.com/jjg0519/p/670274 ...
- c# 粘贴复制
复制 1. 复制 Clipboard.SetText("123456"); Clipboard.SetImage(Image img); Clipboard.SetAudio(Sy ...
- navigateTo防止多次跳转
“wx.navigateTo” 页面跳转.在有网络延迟时多次点击会产生 多次二级页面 再使用wx.navigateBack就会多次返回到之前那页面 解决办法: 点击之前标个状态true 点击之后跳转路 ...
- Xcode8警告⚠️ Empty paragraph passed to '@xxx' command
问题 Xcode8升级后,之前添加的注释会有很多警告 解决方法 基础知识,就是在编译选项中,添加警告屏蔽 解决步骤 显示警告信息 显示警告信息.png 查看警告类型 查看警告类型.png 屏蔽警告 W ...