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循环引用)的更多相关文章

  1. 【STL学习】智能指针之weak_ptr

    简介 weak_ptr是shared_ptr的观察者,它不会干扰shared_ptr所共享对象的所有权,当一个weak_ptr所观察的shared_ptr要释放它的资源时,它会把相关的weak_ptr ...

  2. [6] 智能指针boost::weak_ptr

    [1]boost::weak_ptr简介 boost::weak_ptr属于boost库,定义在namespace boost中,包含头文件 #include<boost/weak_ptr.hp ...

  3. 【C++11新特性】 C++11智能指针之weak_ptr

    如题,我们今天要讲的是C++11引入的三种智能指针中的最后一个:weak_ptr.在学习weak_ptr之前最好对shared_ptr有所了解.如果你还不知道shared_ptr是何物,可以看看我的另 ...

  4. c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)

    一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...

  5. 【校招面试 之 C/C++】第26题 C++ 智能指针(二)之 share_ptr

    1.综述 shared_ptr 是一个标准的共享所有权的智能指针, 允许多个指针指向同一个对象. 定义在 memory 文件中(非memory.h), 命名空间为 std. shared_ptr 是为 ...

  6. 智能指针之 weak_ptr

    1. weak_ptr 介绍 std::weak_ptr 是一种智能指针,它对被 std::shared_ptr 管理的对象存在非拥有性("弱")引用.在访问所引用的对象指针前必须 ...

  7. C++2.0新特性(七)——<Smart Pointer(智能指针)之weak_ptr>

    一.weak_ptr出现的意义 上一节提到过shared_ptr,它会自动释放“不再需要使用的对象”的相应的资源,但是它不是万能的,在某些时候(比如说循环引用),它会显得力不从心,这就是weak_pt ...

  8. c++智能指针的使用,shared_ptr,unique_ptr,weak_ptr

    c++智能指针的使用 官方参考 普通指针的烦恼:内存泄漏,多次释放,提前释放 智能指针 负责自动释放所指向的对象. 三种智能指针 shared_ptr,unique_ptr,weak_ptr: 将sh ...

  9. 深入学习c++--智能指针(一) shared_ptr

    1. 几种智能指针 1. auto_ptr: c++11中推荐不使用他 2. shared_ptr: 每添加一次引用 就+1,减少一次引用,就-1:做到指针进行共享 3. unique_ptr: 一个 ...

随机推荐

  1. ArcMap 制图出图

    在这里用的数据是:台湾2018年6月人口统计数据来做展示数据. ArcMap 里面主要用到的是:符号系统使用.布局视图等 一.属性中设置元素符号 ArcMap 初次加载数据,默认的是“单一符号”,就是 ...

  2. sitemap怎么制作才适合蜘蛛抓取?

    网站sitemap制作格式与要求 1.sitemap格式说明 <?xml version="1.0" encoding="utf-8"?> < ...

  3. 要求用Windows下批处理和Linux下的shell脚本完成,两文本交替输出

    两个短文件 两个空文件 一空一短 两长

  4. java中split函数参数特殊字符的处理(转义),如:"." 、"\"、"|"

    内容介绍 本文主要介绍java中特殊字符做为split函数的参数,如:"." ."\"."|",双引号等,不能正确分隔源字符串的处理方法. ...

  5. SQL Server 默认跟踪(Default Trace)介绍使用

    背景 当数据库的表.存储过程经常别修改,当这些修改造成BUG的时候,很多开发都不承认是他们干的,那我们有没办法找出谁干的呢? SQL Server有Default Trace默认跟踪,数据库记录信息到 ...

  6. c语言数组的概念和指针的加减使用

    //数组变量名:就是一个地址:就是数组首元素的地址#include <stdio.h> int main(void) { int age[5] = {10,50,100,22,44}; / ...

  7. 生活中需要一台mac本子

    用了好多年的windows系统,各种快捷键玩得飞起.当时对mac笔记本的印象就是. mac本子好高大上,搞设计的人才会去用的.(这个也是我听其他人说的) 在当时公司搞IOS开发的人中,面对这个高大上的 ...

  8. 安装wazuh-agent

    安装wazuh-agent 1. windows 下载地址:https://packages.wazuh.com/3.x/windows/wazuh-agent-3.9.5-1.msi 安装运行 设置 ...

  9. UOJ#221. 【NOI2016】循环之美 数论,杜教筛

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ221.html 题解 首先把题目转化为求 \[\sum_{x=1}^n \sum_{y=1}^m [\gcd(x,y) = ...

  10. MySQL5.7 基础之二

    设计范式: 第一范式:字段是原子性 第二范式:存在可用主键 第三范式:任何表都不应该有依赖于其它表非主键的字段 创建数据库.设计数据表 字段:字段名.数据类型.约束(通过键来实现,而键其实可以当做索引 ...