当前位置:网站首页>Scoped in webrtc_ refptr
Scoped in webrtc_ refptr
2022-07-06 09:29:00 【What are you afraid of? The truth is infinite】
C++ The memory applied in the heap in requires the programmer to delete it manually , This is a C++ It is easy to cause the root cause of memory leakage . Have used Java All of our friends know ,Java There is a perfect memory recycling mechanism , There is no need for programmers to call the operation of freeing memory .C++ Is it possible to achieve something similar in Java The mechanism of automatically reclaiming memory ? The answer is yes , and Webrtc Medium scoped_refptr Is to achieve similar functions .
First, we start with the name of this class to understand its meaning . A name consists of three words , Respectively scope,ref,ptr.
First look at ptr,C++ Programmers know this is pointer Pointer means , But the class name contains the semantics of pointers , How to understand this ? The author is not just , Tell the truth , It took me a long time to figure out what happened . Let's start with the conclusion , stay webrtc in , All with ptr Ending class , If you create a non pointer object with this class , Can be used as the syntax of the pointer object . It is estimated that the readers are a little confused , The following is a practical example .
Generally speaking ,C++ There are two ways to create objects , One is to create a pointer object .
A *a = new A();
a->func();
The other is to create non pointer objects :
A a;
a.func()
Objects created by these two methods , The syntax of object calling methods is different . A is -> Way to call , A is . Way to call . But look :
scoped_refptr<A> temp;
temp->func();
Have you noticed ?temp Is created as a non pointer , But it can use the syntax of the pointer object to call function methods . How is this realized ? This is not difficult at all ,scoped_refptr In the code of operator-> The operator of .
T* operator->() const { return ptr_; }
therefore , Like this ptr Ending class , It is best to create objects in a non pointer way . There is another advantage of creating objects in a non pointer way , When an object leaves its scope , It will automatically call the destructor of the object to destroy itself . This is also related to the following scope Words are related .
To understand scope The meaning of , We need to learn a concept , It's called RAII. If you simply look at the full English name of this concept (Resouce Acquisition Is Initialization), It will make people puzzling , I don't know . In fact, it is a pointer to the heap memory space , Stored in a stack space object , Through the stack space object automatic recycling function , At the same time, the method of reclaiming the heap memory pointed to by this pointer . Take a simple example , If we create a pointer object in a section of scope code , When leaving this scope , If the pointer is not passed to another method , At this point, you should explicitly call delete Release the pointer
1.A* a = new A();
2.delete a;
3.a = NULL;
Although for experienced C++ For the programmer , The probability of forgetting to write the last two sentences is not high , But these large amounts of memory free code embedded in business code can not be ignored . So some people think of an advanced way to encapsulate the release of the pointer . Doing this :
class scoped_A{undefined
private :
A *a;
public:
scoped_A(A* a){undefined
this.a = a;
}
~scoped_A(){undefined
delete a;
a = NULL;
}
}
The specific use :
A* a = new A()
scoped_A(a) sa;
perhaps :
scoped_A sa(new A());
Did you see? ? You can understand it as using a scoped_A The object of sa Put the pointer object a encase , When sa Automatically call when leaving the scope scoped_A Destructor of , To achieve automatic release a The purpose of the pointer . Have to say , This is a wonderful way . This is also scope The meaning of , It means this ptr Objects are scoped , Out of scope , Will automatically recycle .
Finally, let's talk about ref,ref It means quoting . stay JAVA in , We often say that a piece of memory will be referenced by multiple variables , When this memory is not referenced by any variables , Memory will be recycled by garbage collection mechanism . stay C++ in , A piece of memory in the heap will also be referenced by multiple pointer variables at the same time ( Point to ), Is there any way like JAVA equally , When this heap memory is not referenced by any pointer , What about automatic recycling ? This is about Webrtc Medium RefCountedObject The class . An explanation of this class can be found in another article . To fully understand scoped_refptr, Need two articles to read together .
summary :scoped_refptr It is a realization in C++ Function class for automatically reclaiming object memory on . It can when the object leaves the scope , Judge whether to reclaim the memory in the heap by referring to the counter , Implemented the similarity Java The mechanism of garbage collection .
边栏推荐
- Basic usage of xargs command
- 五层网络体系结构
- 【shell脚本】——归档文件脚本
- 为拿 Offer,“闭关修炼,相信努力必成大器
- 基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
- Go redis initialization connection
- Redis cluster
- Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
- I-BERT
- Lua script of redis
猜你喜欢
Mapreduce实例(四):自然排序
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
[Yu Yue education] Wuhan University of science and technology securities investment reference
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
Reids之缓存预热、雪崩、穿透
Multivariate cluster analysis
Improved deep embedded clustering with local structure preservation (Idec)
Detailed explanation of cookies and sessions
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
Advanced Computer Network Review(5)——COPE
随机推荐
go-redis之初始化连接
018.有效的回文
YARN组织架构
Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
Global and Chinese market of bank smart cards 2022-2028: Research Report on technology, participants, trends, market size and share
Appears when importing MySQL
O & M, let go of monitoring - let go of yourself
Selenium+pytest automated test framework practice (Part 2)
七层网络体系结构
Leetcode:608 树节点
Leetcode problem solving 2.1.1
One article read, DDD landing database design practice
Redis之Lua脚本
Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)
英雄联盟轮播图自动轮播
五层网络体系结构
【shell脚本】使用菜单命令构建在集群内创建文件夹的脚本
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
Kratos ares microservice framework (III)
Intel distiller Toolkit - Quantitative implementation 1