当前位置:网站首页>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 .
边栏推荐
- Reader writer model
- Codeforces Round #715 (Div. 2) D. Binary Literature
- 游戏商城毕业设计
- Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
- 网络工程师考核的一些常见的问题:WLAN、BGP、交换机
- To be continued] [UE4 notes] L4 object editing
- [to be continued] [UE4 notes] L1 create and configure items
- Control Unit 控制部件
- 个人开发的渗透测试工具Satania v1.2更新
- Palindrome (csp-s-2021-palin) solution
猜你喜欢

sync. Interpretation of mutex source code

Pointnet++学习
![To be continued] [UE4 notes] L4 object editing](/img/0f/cfe788f07423222f9eed90f4cece7d.jpg)
To be continued] [UE4 notes] L4 object editing

Graduation project of game mall

R语言【数据集的导入导出】

lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8

EOJ 2021.10 E. XOR tree

Hang wait lock vs spin lock (where both are used)

剑指 Offer 06.从头到尾打印链表

剑指 Offer 04. 二维数组中的查找
随机推荐
Romance of programmers on Valentine's Day
Developing desktop applications with electron
Acwing 4300. Two operations
Haut OJ 1243: simple mathematical problems
【实战技能】非技术背景经理的技术管理
Haut OJ 1347: addition of choice -- high progress addition
Haut OJ 1221: a tired day
Sword finger offer 05 Replace spaces
Chapter 6 data flow modeling - after class exercises
R语言【数据集的导入导出】
Maximum number of "balloons"
数仓项目的集群脚本
YOLOv5-Shufflenetv2
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Sword finger offer 35 Replication of complex linked list
2017 USP Try-outs C. Coprimes
kubeadm系列-00-overview
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
A new micro ORM open source framework