std::weak_ptr 与 std::shared_ptr 配合使用

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 nulln";

上面结果打印 "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 nulln";
  }
} 

结果,

std::weak_ptr 与 std::shared_ptr 配合使用

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

关于垃圾回收的讨论:

原文链接: https://www.cnblogs.com/strive-sun/p/15979782.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    std::weak_ptr 与 std::shared_ptr 配合使用

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/404545

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月25日 下午4:37
下一篇 2023年4月25日 下午4:37

相关推荐