当前位置:网站首页>shared_ Repeated release heap object of PTR hidden danger
shared_ Repeated release heap object of PTR hidden danger
2022-07-05 05:34:00 【Raise items】
It was thought that smart pointers could completely ensure the correct reference and release of heap objects , But smart pointers are not omnipotent . With shared_ptr For example , The following usage cases will make the code repeatedly release heap objects .
1. Abuse of primitive pointer construction shared_ptr
#include <iostream>
#include <memory>
class A {
};
using Ptr = std::shared_ptr<A>;
int main()
{
A *ps = new A();
Ptr p1(ps);
Ptr p2(p1);
Ptr p3 = p2;
Ptr p4;
p4 = p3;
Ptr p5(ps);
std::cout << p1.use_count() << std::endl;
std::cout << p2.use_count() << std::endl;
std::cout << p3.use_count() << std::endl;
std::cout << p4.use_count() << std::endl;
std::cout << p5.use_count() << std::endl;
return 0;
}
The operation results are as follows :

You can see , In the use of Copy structure Or assignment operator = When ,shared_ptr The control block will be shared and updated . And when you use Primitive pointer construction A new shared_ptr when , A new control block will be created and initialized .
So at the end of the program , When shared_ptr When you want to parse the heap object you point to , Memory will be released repeatedly , This causes runtime errors .
structure
shared_ptrwhen , If the object it points to has been replaced by anothershared_ptrTo refer to , Please initialize the new pointer with copy construction or assignment .
2. The class contains... That points to itself shared_ptr
#include <iostream>
#include <memory>
class A;
using SPtr = std::shared_ptr<A>;
using WPtr = std::weak_ptr<A>;
class A {
public:
A() : sptr(this), wptr(sptr)
{
std::cout << "in construct :" << std::endl;
std::cout << sptr.use_count() << std::endl;
std::cout << this << " " << sptr << " " << wptr.lock() << std::endl;
}
private:
SPtr sptr;
WPtr wptr;
};
int main()
{
A *p = new A();
SPtr ptr(p);
std::cout << "in main :" << std::endl;
std::cout << ptr.use_count() << std::endl;
return 0;
}
The operation results are as follows :
The reason is similar to the above , A heap object By Two shared_ptr Control block management , Eventually, memory is repeatedly released .
If main The function is like this , There will also be running errors . The reason is when the object is delete When released , Object shared_ptr Also released , At this time, the object it points to (this) Will be released again .
int main()
{
A *p = new A();
std::cout << p << std::endl;
delete p;
return 0;
}

Please don't use this wonderful usage .
边栏推荐
- Solution to the palindrome string (Luogu p5041 haoi2009)
- 过拟合与正则化
- YOLOv5添加注意力机制
- Double pointer Foundation
- [article de jailhouse] jailhouse hypervisor
- 数仓项目的集群脚本
- Csp-j-2020-excellent split multiple solutions
- Palindrome (csp-s-2021-palin) solution
- Chapter 6 data flow modeling - after class exercises
- 剑指 Offer 53 - II. 0~n-1中缺失的数字
猜你喜欢

CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five

剑指 Offer 35.复杂链表的复制

sync.Mutex源码解读

A new micro ORM open source framework

【Jailhouse 文章】Jailhouse Hypervisor

Reader writer model

【实战技能】如何做好技术培训?

Sword finger offer 53 - I. find the number I in the sorted array

CF1637E Best Pair

剑指 Offer 09. 用两个栈实现队列
随机推荐
R语言【数据集的导入导出】
常见的最优化方法
[interval problem] 435 Non overlapping interval
用STM32点个灯
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Sword finger offer 53 - I. find the number I in the sorted array
Palindrome (csp-s-2021-palin) solution
To the distance we have been looking for -- film review of "flying house journey"
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
object serialization
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
Summary of Haut OJ 2021 freshman week
剑指 Offer 58 - II. 左旋转字符串
The present is a gift from heaven -- a film review of the journey of the soul
Haut OJ 1221: a tired day
YOLOv5添加注意力机制
[binary search] 69 Square root of X
Web APIs DOM node
Sword finger offer 58 - ii Rotate string left
High precision subtraction