C语言 智能指针 shared_ptr 和 weak_ptr

weak_ptr引入可以解决shared_ptr交叉引用时无法释放资源的问题。

示例代码:

#include <iostream>
#include <memory>

using namespace std;

class B;

class A{
public:
    A(){cout << "A constructor ... "<< endl;}
    ~A(){cout << "A destructor ..." << endl;}
    std::shared_ptr<B> pb;
};

class B{
public:
    B(){cout << "B constructor ... "<< endl;}
    ~B(){cout << "B destructor ..." << endl;}
    std::shared_ptr<A> pa;
};

int main(int argc, char **argv) {
    
    std::shared_ptr<int> a = std::make_shared<int>(3);
    std::shared_ptr<char> b = std::make_shared<char>('a');
    
    std::cout << "shared_ptr object(int) size = " << sizeof(a) << std::endl;
    std::cout << "shared_ptr object(char) size = " << sizeof(b) << std::endl;
    
    std::weak_ptr<A> shadow_a;
    std::weak_ptr<B> shadow_b;
    
    {
    std::shared_ptr<A> ptr_a = std::make_shared<A>();
    std::shared_ptr<B> ptr_b = std::make_shared<B>();
    
    shadow_a = ptr_a;
    shadow_b = ptr_b;
    
    ptr_a->pb = ptr_b;
    ptr_b->pa = ptr_a;
    
    cout << "reference count of A = " << shadow_a.use_count() << endl;
    cout << "reference count of B = " << shadow_b.use_count() << endl;
    cout << endl; 
    }
    
    cout << "reference count of A = " << shadow_a.use_count() << endl;
    cout << "reference count of B = " << shadow_b.use_count() << endl;
    
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

运行代码得到以下输出:

shared_ptr object(int) size = 16
shared_ptr object(char) size = 16
A constructor ... 
B constructor ... 
reference count of A = 2
reference count of B = 2

reference count of A = 1
reference count of B = 1
Hello, world!

从结果可以看出,由于交叉引用导致申请的内存A,B无法正常释放。
为什么会这样呢?这个应该从析构原理进行考虑,shared_ptr引用计数需要为0才会进行析构!但是ptr_a离开作用域会导致A引用计数减少1,但是A的引用计数此时为1,那么 pb不会释放;同理,ptr_b离开作用域会导致B引用计数减少1,但是B的引用计数为此时为1,那么pa不会释放。如此导致了资源无法释放掉。
由于weak_ptr并不会改变shared_ptr的引用计数,所以修改类A,和类B中的shared_ptr对象为weak_ptr对象即可释放资源。

修改后的代码如下:

#include <iostream>
#include <memory>

using namespace std;

class B;

class A{
public:
    A(){cout << "A constructor ... "<< endl;}
    ~A(){cout << "A destructor ..." << endl;}
    //std::shared_ptr<B> pb;
    std::weak_ptr<B> pb;
};

class B{
public:
    B(){cout << "B constructor ... "<< endl;}
    ~B(){cout << "B destructor ..." << endl;}
    //std::shared_ptr<A> pa;
    std::weak_ptr<A> pa;
};

int main(int argc, char **argv) {
    
    std::shared_ptr<int> a = std::make_shared<int>(3);
    std::shared_ptr<char> b = std::make_shared<char>('a');
    
    std::cout << "shared_ptr object(int) size = " << sizeof(a) << std::endl;
    std::cout << "shared_ptr object(char) size = " << sizeof(b) << std::endl;
    
    std::weak_ptr<A> shadow_a;
    std::weak_ptr<B> shadow_b;
    
    {
    std::shared_ptr<A> ptr_a = std::make_shared<A>();
    std::shared_ptr<B> ptr_b = std::make_shared<B>();
    
    shadow_a = ptr_a;
    shadow_b = ptr_b;
    
    ptr_a->pb = ptr_b;
    ptr_b->pa = ptr_a;
    
    cout << "reference count of A = " << shadow_a.use_count() << endl;
    cout << "reference count of B = " << shadow_b.use_count() << endl;
    cout << endl; 
    }
    
    cout << "reference count of A = " << shadow_a.use_count() << endl;
    cout << "reference count of B = " << shadow_b.use_count() << endl;
    
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

运行结果如下,可以正常释放资源。

shared_ptr object(int) size = 16
shared_ptr object(char) size = 16
A constructor ... 
B constructor ... 
reference count of A = 1
reference count of B = 1

B destructor ...
A destructor ...
reference count of A = 0
reference count of B = 0
Hello, world!

到此这篇关于C语言 智能指针 shared_ptr 和 weak_ptr的文章就介绍到这了,更多相关 shared_ptr 和 weak_ptr内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C++11新特性之智能指针(shared_ptr/unique_ptr/weak_ptr)

    shared_ptr基本用法 shared_ptr采用引用计数的方式管理所指向的对象.当有一个新的shared_ptr指向同一个对象时(复制shared_ptr等),引用计数加1.当shared_ptr离开作用域时,引用计数减1.当引用计数为0时,释放所管理的内存. 这样做的好处在于解放了程序员手动释放内存的压力.之前,为了处理程序中的异常情况,往往需要将指针手动封装到类中,通过析构函数来释放动态分配的内存:现在这一过程就可以交给shared_ptr去做了. 一般我们使用make_shared来

  • 浅析Boost智能指针:scoped_ptr shared_ptr weak_ptr

    一. scoped_ptrboost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放.下列代码演示了该指针的基本应用: 复制代码 代码如下: #include <string>#include <iostream>#include <boost/scoped_ptr.hpp> class implementation{public:    ~implementation() { std::cout

  • C++11中的智能指针shared_ptr、weak_ptr源码解析

    目录 1.前言 2.源码准备 3.智能指针概念 4.源码解析 4.1.shared_ptr解析 4.1.1.shared_ptr 4.1.2.__shared_ptr 4.1.3.__shared_count 4.1.4._Sp_counted_base 4.1.5._Sp_counted_ptr 4.1.6.shared_ptr总结 4.2.weak_ptr解析 4.2.1.weak_ptr 4.2.2.__weak_ptr 4.2.3.__weak_count 4.2.4.回过头看weak_

  • C语言 智能指针 shared_ptr 和 weak_ptr

    weak_ptr引入可以解决shared_ptr交叉引用时无法释放资源的问题. 示例代码: #include <iostream> #include <memory> using namespace std; class B; class A{ public:     A(){cout << "A constructor ... "<< endl;}     ~A(){cout << "A destructor ..

  • C++智能指针shared_ptr分析

    C++智能指针shared_ptr分析 概要: shared_ptr是c++智能指针中适用场景多,功能实现较多的智能指针.它采取引用计数的方法来实现释放指针所指向的资源.下面是我代码实现的基本功能. 实例代码: template<class T> class sharedptr { public: sharedptr(T* ptr) :_ptr(ptr) , _refCount(new int(1)) {} sharedptr(sharedptr<T>& sp) :_ptr

  • C++智能指针shared_ptr

    目录 1.什么是shared_ptr? 2.shared_ptr支持哪些操作? 3.如何创建shared_ptr的实例? 4.什么是shared_ptr的引用计数?如何查看? 5.shared_ptr何时释放其所指向的对象? 1.什么是shared_ptr? C++11中包括shared_ptr在内的多种指针,都是模板类型,意味着使用者可以指定想要操作的类型. 创建shared_ptr的方式如下: shared_ptr<int>p1; // p1=NULL 2.shared_ptr支持哪些操作

  • C语言智能指针之weak_ptr浅析

    目录 前言 使用环境 测试过程 现象分析 总结 前言 weak_ptr这个指针天生一副"小弟"的模样,也是在C++11的时候引入的标准库,它的出现完全是为了弥补它老大shared_ptr天生有缺陷的问题,其实相比于上一代的智能指针auto_ptr来说,新进老大shared_ptr可以说近乎完美,但是通过引用计数实现的它,虽然解决了指针独占的问题,但也引来了引用成环的问题,这种问题靠它自己是没办法解决的,所以在C++11的时候将shared_ptr和weak_ptr一起引入了标准库,用来

  • 关于C++智能指针shared_ptr和unique_ptr能否互转问题

    C++中的智能指针最常用的是shared_ptr和unique_ptr,C++新手最常问的问题是我从一个函数中拿到unique_ptr,但要转成shared_ptr才能使用,要怎么转换?同理是否能将shared_ptr转换成unique_ptr? 我们先简单看看shared_ptr是什么. std::shared_ptr<Widget> a = std::make_shared<Widget>(); 这句代码会在栈中创建一个shared_ptr对象,其最基本的2个指针,一个指向在堆

  • C++中Boost的智能指针shared_ptr

    boost::scoped_ptr虽然简单易用,但它不能共享所有权的特性却大大限制了其使用范围,而boost::shared_ptr可以解决这一局限.顾名思义,boost::shared_ptr是可以共享所有权的智能指针,首先让我们通过一个例子看看它的基本用法: #include <string> #include <iostream> #include <boost/shared_ptr.hpp> class implementation { public: ~imp

  • C++11 智能指针之shared_ptr代码详解

    C++中的智能指针首先出现在"准"标准库boost中. 随着使用的人越来越多,为了让开发人员更方便.更安全的使用动态内存,C++11也引入了智能指针来管理动态对象. 在新标准中,主要提供了shared_ptr.unique_ptr.weak_ptr三种不同类型的智能指针. 接下来的几篇文章,我们就来总结一下这些智能指针的使用. 今天,我们先来看看shared_ptr智能指针. shared_ptr 智能指针 shared_ptr是一个引用计数智能指针,用于共享对象的所有权也就是说它允许

  • 深入学习C++智能指针之shared_ptr与右值引用的方法

    目录 1. 介绍 2. 初始化方法 2.1 通过构造函数初始化 2.2 通过拷贝和移动构造函数初始化 2.3 通过 std::make_shared 初始化 2.4 通过 reset 方法初始化 3. 获取原始指针 4. 指定删除器 5. 参考链接 1. 介绍 在 C++ 中没有垃圾回收机制,必须自己释放分配的内存,否则就会造成内存泄露.解决这个问题最有效的方法是使用智能指针(smart pointer).智能指针是存储指向动态分配(堆)对象指针的类,用于生存期的控制,能够确保在离开指针所在作用

随机推荐