当前位置:网站首页>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 .
边栏推荐
- Design and implementation of online shopping system based on Web (attached: source code paper SQL file)
- Different data-driven code executes the same test scenario
- Pytest parameterization some tips you don't know / pytest you don't know
- Multivariate cluster analysis
- Advanced Computer Network Review(5)——COPE
- Mapreduce实例(九):Reduce端join
- Detailed explanation of cookies and sessions
- Global and Chinese market of airport kiosks 2022-2028: Research Report on technology, participants, trends, market size and share
- Connexion d'initialisation pour go redis
- Redis之核心配置
猜你喜欢

一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?

Mapreduce实例(十):ChainMapReduce

Sentinel mode of redis

Full stack development of quartz distributed timed task scheduling cluster
![[oc]- < getting started with UI> -- common controls uibutton](/img/4d/f5a62671068b26ef43f1101981c7bb.png)
[oc]- < getting started with UI> -- common controls uibutton

Redis cluster

软件负载均衡和硬件负载均衡的选择
![[three storage methods of graph] just use adjacency matrix to go out](/img/79/337ee452d12ad477e6b7cb6b359027.png)
[three storage methods of graph] just use adjacency matrix to go out

Redis之Lua脚本

Redis之连接redis服务命令
随机推荐
Redis之五大基础数据结构深入、应用场景
CSP salary calculation
Global and Chinese market of linear regulators 2022-2028: Research Report on technology, participants, trends, market size and share
五层网络体系结构
为什么要数据分层
【shell脚本】使用菜单命令构建在集群内创建文件夹的脚本
Nacos installation and service registration
Servlet learning diary 8 - servlet life cycle and thread safety
Reids之缓存预热、雪崩、穿透
Global and Chinese markets for small seed seeders 2022-2028: Research Report on technology, participants, trends, market size and share
Multivariate cluster analysis
英雄联盟轮播图自动轮播
一文读懂,DDD落地数据库设计实战
Redis cluster
小白带你重游Spark生态圈!
Sentinel mode of redis
Redis之核心配置
Connexion d'initialisation pour go redis
Servlet learning diary 7 -- servlet forwarding and redirection
[OC foundation framework] - [set array]