std::shared_ptr<int> a = std::make_shared<int>(2);
std::weak_ptr<int> b = a;
a = nullptr;
if (std::shared_ptr<int> b_lock = b.lock())
std::cout << *b_lock;
else
std::cout << "b is null\n";

上面结果打印 "b is null"

weak_ptr 不进行计数,并且不能操作内存,当前赋值的 shared_ptr 销毁后,weak_ptr 也会置空

weak_ptr works together with shared_ptr. When using both, it's still true that the object the pointers point at normally gets destroyed as soon as and only when no shared_ptr points at it any more.

But also, when that object is destroyed, any weak_ptr pointers which were pointing at it automatically change to act like null pointers.

The b.lock() call and shared_ptr b_lock are needed because you can't use a weak_ptr directly, as with *b. This is a safety feature, because if you wrote code to check that b is not null and then

later uses *b, what if some function you call between the check and the use (or some other thread!) happens to destroy or change a? Instead we use lock() to convert the weak_ptr back to a local 

shared_ptr, check whether that pointer is null, and use the shared_ptr. The local shared_ptr guarantees the object will continue living long enough for the code that's about to use it, but doesn't need to stick around after that.

有关其他智能指针的使用可以参阅:


weak_ptr 介绍:

weak_ptr 是一种不控制对象生命周期的智能指针, 它指向一个 shared_ptr 管理的对象. 进行该对象的内存管理的是那个强引用的 shared_ptr. weak_ptr只是提供了对管理对象的一个访问手段.

weak_ptr 设计的目的是为配合 shared_ptr 而引入的一种智能指针来协助 shared_ptr 工作, 它只可以从一个 shared_ptr 或另一个 weak_ptr 对象构造, 它的构造和析构不会引起引用记数的增加或减少.

weak_ptr 在功能上类似于普通指针, 然而一个比较大的区别是, 弱引用能检测到所管理的对象是否已经被释放, 从而避免访问非法内存。

weak_ptr 没有重载*和->但可以使用 lock 获得一个可用的 shared_ptr 对象

比如,

#include <iostream>

int main() {
auto shared = std::make_shared<int>(1); auto weak = std::weak_ptr<int>{shared};
auto w = weak; std::shared_ptr<int> ww = w.lock();
std::cout << std::boolalpha << "shared.use_count(): " << shared.use_count()
<< '\n'
<< "weak.use_count(): " << weak.use_count() << '\n'
<< "weak.expired(): " << weak.expired() << '\n'
<< "w.use_count(): " << w.use_count() << '\n'; shared.reset(); std::cout << "weak.reset();\n"
<< "shared.use_count(): " << shared.use_count() << '\n'
<< "weak.use_count(): " << weak.use_count() << '\n'
<< "weak.expired(): " << weak.expired() << '\n'
<< "w.use_count(): " << w.use_count() << '\n'
<< "w is " << *ww << '\n'; std::shared_ptr<int> a = std::make_shared<int>(2);
std::weak_ptr<int> b = a;
a = nullptr;
if (std::shared_ptr<int> b_lock = b.lock()) {
std::cout << *b_lock << std::endl;
} else {
std::cout << "b is null\n";
}
} 

结果,

注意: 虽然通过弱引用指针可以有效的解除循环引用, 但这种方式必须在程序员能预见会出现循环引用的情况下才能使用, 也可以是说这个仅仅是一种编译期的解决方案, 如果程序在运行过程中出现了循环引用, 还是会造成内存泄漏.

关于垃圾回收的讨论:

std::weak_ptr 与 std::shared_ptr 配合使用的更多相关文章

  1. Item 20: 使用std::weak_ptr替换会造成指针悬挂的类std::shared_ptr指针

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 矛盾的是,我们很容易就能创造出一个和std::shared_ptr ...

  2. std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题

    在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...

  3. 智能指针std::weak_ptr

    std::weak_ptr 避免shared_ptr内存泄漏的利器.

  4. std::weak_ptr

    weak_ptr 是一种不控制对象生命周期的智能指针, 它指向一个 shared_ptr 管理的对象. 进行该对象的内存管理的是那个强引用的 shared_ptr. weak_ptr只是提供了对管理对 ...

  5. Item 21: 比起直接使用new优先使用std::make_unique和std::make_shared

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 让我们先从std::make_unique和std::make_s ...

  6. 深入学习c++--智能指针(二) weak_ptr(打破shared_ptr循环引用)

    1. 几种智能指针 1. auto_ptr: c++11中推荐不使用他(放弃) 2. shared_ptr: 拥有共享对象所有权语义的智能指针 3. unique_ptr: 拥有独有对象所有权语义的智 ...

  7. C++11 使用异步编程std::async和std::future

    先说明一点:std::asyanc是std::future的高级封装, 一般我们不会直接使用std::futrue,而是使用对std::future的高级封装std::async. 下面分别说一下. ...

  8. 【C++并发实战】(三) std::future和std::promise

    std::future和std::promise std::future std::future期待一个返回,从一个异步调用的角度来说,future更像是执行函数的返回值,C++标准库使用std::f ...

  9. C++0x,std::move和std::forward解析

    1.std::move 1.1std::move是如何定义的 template<typename _Tp> constexpr typename std::remove_reference ...

  10. std::unique_lock与std::lock_guard分析

    背景 C++多线程编程中通常会对共享的数据进行写保护,以防止多线程在对共享数据成员进行读写时造成资源争抢,导致程序出现未定义或异常行为.通常的做法是在修改共享数据成员时进行加锁(mutex).在使用锁 ...

随机推荐

  1. [转帖]tidb Modify Configuration Dynamically

    https://docs.pingcap.com/tidb/v6.5/dynamic-config This document describes how to dynamically modify ...

  2. [转帖]Dockerfile中CMD和ENTRYPOINT命令详解

    https://www.jb51.net/article/136264.htm   Dockerfile中的ENTRYPOINT指令和CMD指令都可以设置容器启动时要执行的命令,但用途是有略微不同的. ...

  3. [转帖]Linux Shell:date日期时间操作

    https://www.jianshu.com/p/cc9ebb212a8e 整理Linux Shell脚本中常用的日期操作,给予date命令,主要用法总结 获得当前日期,时间戳,date,date ...

  4. Firewall-cmd 启用NAT开放内网机器通过能上网的机器访问外网的方法

    Firewall-cmd 启用NAT开放内网机器通过能上网的机器访问外网的方法 背景说明 昨天的处置已经可以让外部可以使用唯一的电信IP地址访问内部交换机的服务器了. 但是今天的场景是想让内网交换机的 ...

  5. Redisson/Jedis 线程数不足报错问题的思考

    Redisson/Jedis 线程数不足报错问题的思考 背景 最近公司内总出现 Redis相关的错误 !-_-! 看我最近发的博客就可以看的出来. 这个错误提示其实是 两年前 清明节进行 压测时发现的 ...

  6. 【验证码逆向专栏】最新某度旋转验证码 v2 逆向分析

    声明 本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容.敏感网址.数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关! 本文章未经许 ...

  7. 2020美亚个人赛wp

    案例背景 2020年9月,数名信用卡持有人向警方报案,指他们的信用卡被不知名人士在一家本地网上商店购买手机.订单大部分来自海外的网络地址,但有一宗订单来自本地.警方经调查后发现该本地网络地址的注册地址 ...

  8. Golang Map底层实现简述

    Go的map是一种高效的数据结构,用于存储键值对.其底层实现是一个哈希表(hash table),下面是有关map底层实现的详细介绍: 哈希表: map的底层实现是一个哈希表,也称为散列表.哈希表是一 ...

  9. C# 使用字典将枚举转换为String

    枚举 public enum ColorType { Red = 10, Blue = 20, Green = 30, Yellow = 40, } String var A1 = "AAA ...

  10. 多模态对比语言图像预训练CLIP:打破语言与视觉的界限

    多模态对比语言图像预训练CLIP:打破语言与视觉的界限 一种基于多模态(图像.文本)对比训练的神经网络.它可以在给定图像的情况下,使用自然语言来预测最相关的文本片段,而无需为特定任务进行优化.CLIP ...