当前位置:网站首页>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 .
边栏推荐
- [shell script] use menu commands to build scripts for creating folders in the cluster
- [oc foundation framework] - < copy object copy >
- Kratos战神微服务框架(一)
- Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
- 一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
- Global and Chinese markets for hardware based encryption 2022-2028: Research Report on technology, participants, trends, market size and share
- 不同的数据驱动代码执行相同的测试场景
- Redis之哨兵模式
- 基于B/S的医院管理住院系统的研究与实现(附:源码 论文 sql文件)
- Redis之Lua脚本
猜你喜欢
Detailed explanation of cookies and sessions
QDialog
Research and implementation of hospital management inpatient system based on b/s (attached: source code paper SQL file)
[oc foundation framework] - < copy object copy >
Heap (priority queue) topic
Multivariate cluster analysis
Redis geospatial
Master slave replication of redis
什么是MySQL?MySql的学习之路是怎样的
Redis之Bitmap
随机推荐
[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology
Go redis initialization connection
Redis之主从复制
Redis之发布订阅
Kratos ares microservice framework (I)
Redis之连接redis服务命令
[oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
Kratos战神微服务框架(一)
英雄联盟轮播图手动轮播
Meituan Er Mian: why does redis have sentinels?
Mapreduce实例(九):Reduce端join
Kratos ares microservice framework (III)
YARN组织架构
七层网络体系结构
一篇文章带你了解-selenium工作原理详解
Redis之Bitmap
Post training quantification of bminf
一文读懂,DDD落地数据库设计实战
Leetcode problem solving 2.1.1
Global and Chinese market of cup masks 2022-2028: Research Report on technology, participants, trends, market size and share