深入学习c++--智能指针(二) weak_ptr(打破shared_ptr循环引用)
1. 几种智能指针
1. auto_ptr: c++11中推荐不使用他(放弃)
2. shared_ptr: 拥有共享对象所有权语义的智能指针
3. unique_ptr: 拥有独有对象所有权语义的智能指针
4. weaked_ptr: 到 std::shared_ptr 所管理对象的弱引用
1.1 weak_ptr
参考:https://zh.cppreference.com/w/cpp/memory/weak_ptr
std::weak_ptr是一种智能指针,它对被 std::shared_ptr 管理的对象存在 非拥有性(“弱”)引用。在访问所引用的对象前必须 先转换为 std::shared_ptr。std::weak_ptr用来 表达临时所有权的概念:当某个对象只有存在时才需要被访问,而且随时可能被他人删除时,可以使用
std::weak_ptr来跟踪该对象。需要获得临时所有权时,则将其转换为 std::shared_ptr,此时如果原来的 std::shared_ptr被销毁,则该对象的生命期将被延长至这个临时的 std::shared_ptr 同样被销毁为止。
std::weak_ptr 的另一用法是:打断 std::shared_ptr 所管理的对象组成的环状引用。(打破shared_ptr的循环引用)
若这种环被孤立(例如无指向环中的外部共享指针),则
shared_ptr引用计数无法抵达零,而内存被泄露。能令环中的指针之一为弱指针以避免此情况。
循环引用的问题:该被调用的析构函数没有被调用
#include <iostream>
#include <memory>
using namespace std; class Parent;
typedef std::shared_ptr<Parent> ParentPtr; class Child
{
public:
ParentPtr father;
Child() {
cout << "hello Child" << endl;
}
~Child() {
cout << "bye Child\n";
}
}; typedef std::shared_ptr<Child> ChildPtr; class Parent {
public:
ChildPtr son;
Parent() {
cout << "hello parent\n";
}
~Parent() {
cout << "bye Parent\n";
}
}; void testParentAndChild()
{
ParentPtr p(new Parent());
ChildPtr c(new Child());
p->son = c;
c->father = p;
} int main()
{
testParentAndChild();
return ;
}

问题:c只有调用p的析构的时候,才能被释放。p只有调用c的析构的时候,才能被释放。。形成了循环引用,造成了内存泄露
因此,引入新的智能指针,weak_ptr
1.2 weak_ptr基本用法
一旦外部指向shared_ptr资源失效,那么weak_ptr管理的资源自动失效
#include <iostream>
#include <cassert>
#include <memory>
using namespace std; class Object
{
public:
Object(int id) : m_id(id) {
std::cout << "init obj " << m_id << std::endl;
}
~Object() {
std::cout << "bye bye " << m_id << std::endl;
}
int id() const {
return m_id;
}
private:
int m_id;
}; void sharedPtrWithWeakPtr()
{
typedef std::shared_ptr<Object> ObjectPtr;
typedef weak_ptr<Object> WeakObjectPtr; ObjectPtr obj(new Object()); // 一旦外部指向shared_ptr资源失效,那么weak_ptr管理的资源自动失效 WeakObjectPtr weakObj2; // 裸指针
WeakObjectPtr weakObj(obj); // 指向shared_ptr指针
WeakObjectPtr weakObj3(obj);
cout << "obj use count is " << obj.use_count() << endl; // 1--尽管weak_ptr也指向obj,但他只是监听者,本身并不影响引用计数次数
{
// weak_ptr使用方法
// 外部至少还有一个shared_ptr来管理资源,同时p自己本身的资源 -- p.use_count >= 2
auto p = weakObj.lock(); // auto == ObjectPtr
if (p) {
cout << p.unique() << endl; // 0 -- p.use_count() >= 2
}
else {
}
}
// 不指向某份资源
// obj.reset();
// {
// auto p = weakObj.lock(); // auto == ObjectPtr
// if (p) {
// assert(false);
// }
// else {
// cout << "null" << endl; // 如果调用reset,就会执行这句话
// }
// } // 此时, Object(1)已经失效
obj.reset(new Object()) ;
{
auto p = weakObj.lock(); // 返回值如果有效, 在外部必须有weakObj指向同一个资源
if (p) {
assert(false);
}
else {
cout << "null " << endl; // null
}
} weakObj = obj; // 又有效了
// 想知道weak_ptr有没有管理一份资源(外部有没有资源), 又不想生成一个shared_ptr
if (weakObj.expired()) {
// 资源过期了 -- true
cout << "no ptr" << endl;
}
else {
cout << "have resource\n";
}
} int main()
{ sharedPtrWithWeakPtr();
return ; }

1.3 解决类之间循环引用
一旦外部指向shared_ptr资源失效,那么weak_ptr管理的资源自动失效
#include <iostream>
#include <cassert>
#include <memory>
using namespace std; class Parent;
typedef std::shared_ptr<Parent> ParentPtr;
typedef std::weak_ptr<Parent> WeakParentPtr; class Child
{
public:
WeakParentPtr father; // 只有一环换成 weak_ptr, 即可打破环
Child() {
cout << "hello Child" << endl;
}
~Child() {
cout << "bye Child\n";
}
}; typedef std::shared_ptr<Child> ChildPtr;
typedef std::weak_ptr<Child> WeakChildPtr; class Parent {
public:
ChildPtr son;
Parent() {
cout << "hello parent\n";
}
~Parent() {
cout << "bye Parent\n";
}
}; void testParentAndChild()
{
ParentPtr p(new Parent());
ChildPtr c(new Child());
p->son = c;
c->father = p;
cout << (c->father).use_count() << endl;
cout << (p->son).use_count() << endl;
} int main()
{
testParentAndChild();
return ;
}

析构函数成功调用, 注意析构顺序,谁是weak_ptr对象,就先析构谁。
1.4 用 enable_shared_from_this从this转换到shared_ptr
类中函数接口需要一个本对象智能指针的const引用 (如何生成本对象的智能指针)
用 enable_shared_from_this从this转换到shared_ptr (使用CRTP来使用 本对象的智能指针)
主要代码:
handleChildAndParent(shared_from_this(), ps);
完整代码:
#include <iostream>
#include <cassert>
#include <memory>
using namespace std; class Parent;
typedef std::shared_ptr<Parent> ParentPtr;
typedef std::weak_ptr<Parent> WeakParentPtr; class Child : public std::enable_shared_from_this<Child> // 奇异模板参数 CRTP
{
public:
WeakParentPtr father; // 只有一环换成 weak_ptr, 即可打破环
Child() {
cout << "hello Child" << endl;
}
~Child() {
cout << "bye Child\n";
}
void CheckRelation() { }
}; typedef std::shared_ptr<Child> ChildPtr;
typedef std::weak_ptr<Child> WeakChildPtr; void handleChildAndParent(const ParentPtr& p, const ChildPtr& c); class Parent : public enable_shared_from_this<Parent> {
public:
WeakChildPtr son;
Parent() {
cout << "hello parent\n";
}
~Parent() {
cout << "bye Parent\n";
}
void CheckRelation() {
auto ps = son.lock();
if (ps) {
//原理: 类中存在weak_ptr指针,通过一系列方式转换成 shared_ptr,然后传参
handleChildAndParent(shared_from_this(), ps); // 使用CRTP来使用 本对象的指针 ★ ★ ★ ★
}
cout << "after call checkRelation\n";
}
}; void testParentAndChild()
{
ParentPtr p(new Parent());
ChildPtr c(new Child());
p->son = c;
c->father = p;
cout << (c->father).use_count() << endl;
cout << (p->son).use_count() << endl; p->CheckRelation();
} void handleChildAndParentRef(const Parent& p, const Child& c)
{
auto cp = c.father.lock();
auto pc = p.son.lock();
if (cp.get() == &p && pc.get() == &c)
{
cout << "right relation" << endl;
}
else {
cout << "oop !!!\n";
}
} // const xxx&: 减少拷贝次数
void handleChildAndParent(const ParentPtr& p, const ChildPtr& c)
{
auto cp = c->father.lock();
auto pc = p->son.lock();
if (cp == p && pc == c)
{
cout << "right relation\n";
}
else {
cout << "oop!!!\n";
}
} int main()
{
testParentAndChild();
return ;
}

深入学习c++--智能指针(二) weak_ptr(打破shared_ptr循环引用)的更多相关文章
- 【STL学习】智能指针之weak_ptr
简介 weak_ptr是shared_ptr的观察者,它不会干扰shared_ptr所共享对象的所有权,当一个weak_ptr所观察的shared_ptr要释放它的资源时,它会把相关的weak_ptr ...
- [6] 智能指针boost::weak_ptr
[1]boost::weak_ptr简介 boost::weak_ptr属于boost库,定义在namespace boost中,包含头文件 #include<boost/weak_ptr.hp ...
- 【C++11新特性】 C++11智能指针之weak_ptr
如题,我们今天要讲的是C++11引入的三种智能指针中的最后一个:weak_ptr.在学习weak_ptr之前最好对shared_ptr有所了解.如果你还不知道shared_ptr是何物,可以看看我的另 ...
- c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)
一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...
- 【校招面试 之 C/C++】第26题 C++ 智能指针(二)之 share_ptr
1.综述 shared_ptr 是一个标准的共享所有权的智能指针, 允许多个指针指向同一个对象. 定义在 memory 文件中(非memory.h), 命名空间为 std. shared_ptr 是为 ...
- 智能指针之 weak_ptr
1. weak_ptr 介绍 std::weak_ptr 是一种智能指针,它对被 std::shared_ptr 管理的对象存在非拥有性("弱")引用.在访问所引用的对象指针前必须 ...
- C++2.0新特性(七)——<Smart Pointer(智能指针)之weak_ptr>
一.weak_ptr出现的意义 上一节提到过shared_ptr,它会自动释放“不再需要使用的对象”的相应的资源,但是它不是万能的,在某些时候(比如说循环引用),它会显得力不从心,这就是weak_pt ...
- c++智能指针的使用,shared_ptr,unique_ptr,weak_ptr
c++智能指针的使用 官方参考 普通指针的烦恼:内存泄漏,多次释放,提前释放 智能指针 负责自动释放所指向的对象. 三种智能指针 shared_ptr,unique_ptr,weak_ptr: 将sh ...
- 深入学习c++--智能指针(一) shared_ptr
1. 几种智能指针 1. auto_ptr: c++11中推荐不使用他 2. shared_ptr: 每添加一次引用 就+1,减少一次引用,就-1:做到指针进行共享 3. unique_ptr: 一个 ...
随机推荐
- python_并发编程——队列
1.队列 from multiprocessing import Queue q = Queue(5) #创建队列对象,队列大小为5,队列中只能存放5个元素 q.put(1) #往队列中添加元素 q. ...
- 「CQOI2015」选数
「CQOI2015」选数 题目描述 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都 ...
- 大数相加和大数相乘以及打印从1到最大的n位数
string add(string a, string b){ int nlength; int diff; if (a.size() > b.size()){ nlength = a.size ...
- matlab的正则表达式
第一部分——单个字符的匹配1 句点符号 '.' ——匹配任意一个(只有一个)字符(包括空格).例如:t.n,它匹配tan. ten.tin和ton,还匹配t#n.tpn甚至t nMatlab例子程序: ...
- Java 通过HttpClient Post方式提交json请求
package com.sinosoft.ap.harmfullibrary.util; /** * 发送post请求 */import net.sf.json.JSONObject; import ...
- Oracle查询一个命名空间下所有表和视图的表名、字段名、字段类型、字段大小,是否可为NULL,主键和注释信息
使用SQL查询Oracle一个命名空间下所有表和视图的表名.字段名.字段类型.字段大小,是否可为NULL,主键和注释信息. SQL如下,注意需要将'CDFLOOD'更换为您要查询的命名空间: sele ...
- 如何修改host
因不可抗拒的原因,有些网站会被q,但只是比较恶心的域名DNS污染,并不需要tiizi,修改hosts文件即可. 以 www.youneed.win 为例: 首先,进入目录:C:\Windows\Sys ...
- LightOJ - 1299 - Fantasy Cricket(DP, 数学)
链接: https://vjudge.net/problem/LightOJ-1299 题意: 考虑成,U位置的点要往后放,D位置往前放 Dp[i][j]表示处于i位置,还有j个U没有放下. s[i] ...
- Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)
链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...
- ELK实践
一.ElasticSearch+FileBeat+Kibana搭建平台 在C# 里面运行程序,输出日志(xxx.log 文本文件)到FileBeat配置的路径下面. 平台搭建,参考之前的随笔. Fil ...