智能指针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 ...
随机推荐
- [OpenBenchMarking] AMD CPU 的性能简单对比
来源: https://openbenchmarking.org/result/1710193-AL-EPYC7351P64 1. 2. 3. 4.
- thinkphp5.1路由设置小计
route下定义路由路径,如果是这种情况 'product'=>'home/product/index',//产品信息首页 'product/list'=>'home/product/li ...
- 使用rsync工具构建php项目管理平台
对于phper来说部署项目和更新项目是很方便的,只要直接将写好的项目覆盖到项目的根目录就可以啦.但是平时项目开发的时候肯定不是只部署一个环境,一般是三套环境(开发环境.测试环境.生产环境),我们每次在 ...
- 在Linux上安装和配置CSF(配置服务器安全和防火墙)
CSF是国外一家小公司开发的linux系统免费防火墙,它基于iptables工作,能有效缓解服务器压力,具有自动屏蔽暴力破解密码IP.管理开放端口.免疫轻量DDos和CC等等功能,同时,安装和使用也极 ...
- springMVC接受json类型数据
springMVC接受json格式的数据很简单 使用@RequestBody 注解,标识从请求的body中取值 服务端示例代码 @RequestMapping(value = "/t4&qu ...
- luogu题解 P3763 【[TJOI2017]DNA】
题目链接: https://www.luogu.org/problemnew/show/P3763 思路: 首先我们要用到Rabin-Karp哈希,其实就是这个: 若\(w_{str}\)=(\(a_ ...
- spark2.0的10个特性介绍
1. Spark 2.0 ! 还记得我们的第七篇 Spark 博文里吗?里面我用三点来总结 spark dataframe 的好处: 当时是主要介绍 spark 里的 dataframe,今天是想总结 ...
- 安卓开发之SimpleAdapter的使用
package com.lidaochen.test; import android.support.v7.app.AppCompatActivity; import android.os.Bundl ...
- LLVM使用其他Pass的结果
之前的工作一直集中在clang中,最近有点空闲时间,又重新熟悉了一下Pass的书写过程.(参考LLVM CookBook和http://llvm.org/docs/WritingAnLLVMPass. ...
- 6.B+Tree 检索原理
B+树的创建(索引的创建) 1.比如为phoneNum创建了一个索引,phoneNum这列保存了很多的手机号码 2.索引创建的过程中,会为这些数据进行适当的编码(根据这个数据所在的物理地址),如 36 ...