当前位置:网站首页>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 .
边栏推荐
- [three storage methods of graph] just use adjacency matrix to go out
- Redis之核心配置
- 基于B/S的医院管理住院系统的研究与实现(附:源码 论文 sql文件)
- 【每日一题】搬运工 (DFS / DP)
- Redis之Geospatial
- Redis geospatial
- Redis cluster
- Redis之Bitmap
- Connexion d'initialisation pour go redis
- An article takes you to understand the working principle of selenium in detail
猜你喜欢
Full stack development of quartz distributed timed task scheduling cluster
解决小文件处过多
Research and implementation of hospital management inpatient system based on b/s (attached: source code paper SQL file)
QML control type: Popup
Pytest之收集用例规则与运行指定用例
英雄联盟轮播图自动轮播
Redis之Bitmap
Kratos战神微服务框架(一)
Pytest parameterization some tips you don't know / pytest you don't know
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
随机推荐
Parameterization of postman
Reids之删除策略
Mapreduce实例(九):Reduce端join
[oc]- < getting started with UI> -- learning common controls
Advanced Computer Network Review(3)——BBR
Persistence practice of redis (Linux version)
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
Selenium+Pytest自动化测试框架实战(下)
MapReduce工作机制
QDialog
Global and Chinese market of metallized flexible packaging 2022-2028: Research Report on technology, participants, trends, market size and share
Redis core configuration
【图的三大存储方式】只会用邻接矩阵就out了
[OC foundation framework] - [set array]
Kratos ares microservice framework (I)
[OC foundation framework] - string and date and time >
Selenium+pytest automated test framework practice (Part 2)
Mapreduce实例(五):二次排序
AcWing 2456. Notepad
Global and Chinese markets for hardware based encryption 2022-2028: Research Report on technology, participants, trends, market size and share