網站首頁 編程語言 正文
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 = 2reference 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 = 1B destructor ...
A destructor ...
reference count of A = 0
reference count of B = 0
Hello, world!
原文鏈接:https://blog.csdn.net/lybhit/article/details/122799780
相關推薦
- 2023-07-14 express+mongoose實現無限級分類
- 2023-04-19 清楚詳解Android?進程間圖傳遞圖形buffer原理_Android
- 2022-12-27 pytorch?K折交叉驗證過程說明及實現方式_python
- 2022-09-05 C語言深入淺出分析函數指針_C 語言
- 2022-08-31 Android無障礙監聽通知的實戰過程_Android
- 2022-07-27 C++中整形與浮點型如何在內存中的存儲詳解_C 語言
- 2022-07-24 C++超詳細實現二叉樹的遍歷_C 語言
- 2022-09-22 在容器內獲取 Pod 信息
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支