Smart pointer with weak reference details

  • 2020-05-17 06:33:23
  • OfStack

template, which is widely seen in android < typename T > The class Sp handle class is actually a smart pointer to android to implement a garbage collection mechanism. Smart Pointers is a concept in c + +, because c + + itself does not have the garbage collection mechanism, and pointer also does not have a constructor and destructor, so in order to achieve the memory storage areas (dynamic) safe recycling, must to 1 layer encapsulation of Pointers, and the encapsulation is smart Pointers, actually to put it bluntly, smart pointer is a pointer function at the same time to provide a safe memory recovery of a class. Of course, the function of smart pointer is not only these, want to know more you can go to study ~

There are many ways to implement a smart pointer, and the sp handle class in android is actually a strong-referenced smart pointer to the google implementation. I haven't looked closely at the implementation of android sp, but its basic principles are fixed. Now let's look at the implementation of smart Pointers from a relatively simple example:

First, let's look at the simplest encapsulation of pointer:


Template <typename T> 
class SmartPtr{ 
public: 
SmartPtr(T *p = 0):ptr(p){} 
~SmartPtr(){delete ptr ;} 
private: 
T  * ptr ; 
}; 

With the above encapsulation, we can use SmartPtr in the following ways without worrying about memory leaks:

SmartPtr < int > pointer(new int) ;
*(pointer.ptr) = 10 ;

For ease of use, we can overload the operator to make the smart pointer operate more like a pointer:

T & operator * () * ptr} {return
T* operator- > (){return ptr}

With the overloading above, we can use real pointer 1 without having to solve the memory leak problem.

Because a smart pointer encapsulates a pointer, it must be overloaded to implement a copy constructor and the "=" operator. Because if these two functions are not provided, when the above smart pointer is assigned, the pointer will definitely point to the same region, and 1 denator will result in the same pointer being delete twice.

Here, the implementation of the copy constructor is tricky. When you use the copy constructor to create a new pointer, instead of creating a new object, you make the new smart pointer point to the same object. However, this will result in multiple Pointers to the same block of memory region. How to ensure that the same block of memory region will only be used once by delete when the destructor is called? There are many methods to implement here, the most commonly used is argument control. In other words, a counter is added in the smart pointer, and a strong reference to the memory area is added every time, then the counter is added by 1. When the counter is 0, the object can be deleted. This counter is stored separately with the pointer by dynamic allocation, because this counter is Shared by multiple smart Pointers:


Template <typename T> 
class SmartPtr{ 
public: 
SmartPtr(T *p = 0):ptr(p){count = new int(1) ;}//  The first 1 The second creation, the argument must be 1 
SmartPtr(const SmartPtr & rhs):ptr(rhs.ptr),count(rhs.count){++*rhs.count ;} 
T  & operator*(){return *ptr} 
T* operator->(){return ptr} 
SmartPtr  & operator=(const SmartPtr & rhs){ 
if(ptr == rhs.ptr) 
return *this ; 
if(--*count == 0){ 
delete ptr ; 
delete count ; 
} 
++*rhs.count ; 
count = rhs.count ; 
ptr = rhs.ptr ; 
} 
~SmartPtr(){ 
if(--*count==0) 
delete ptr ; 
delete count ; 
} 
private: 
T  * ptr ; 
int  * count ; 
}; 

In this way, a smart Pointers to the basic forming, of course this is only the most simple smart Pointers, commercial libraries provide smart Pointers are very powerful features, if can carefully study the android implementation in this respect, should have wonderful progress in c + + memory control ~ ~ don't have the time, yet also look down camera, cattle people understood email yes ah ~ ~

Out of the smart pointer sp, android wp this pointer class emerged in the inside, in fact, he was a weak reference type a pointer to the class, a weak reference is in. net and java, often used in a weak reference is a holder of the object reference, using weak references can be maintained for object references, but does not prevent it being garbage collection. If an object has only weak references, it becomes a candidate for garbage collection, just as there is no remaining reference 1, and all weak references are cleared once the object is deleted. Weak references are suitable for classes that have a large number of data members and are relatively easy to recreate, also known as the fat man class. Establishing weak references can refer to objects without preventing them from being garbage collected, thus achieving a certain balance in the use of memory.

In android, the promote function in the wp class is actually upgrading a weak reference to a strong one. Whether it's sp or wp, it's actually a way for android to take advantage of the existing c++ feature to address memory usage and recycling.


Related articles: