Mbase points to refbase
Mrefs points to weakref_impl
In terms of perception, smart pointers are used on new objects. Because New allocates a fast memory and returns a pointer, the system does not know that the user no longer uses this memory. Therefore, only the user explicitly releases the memory,
This may cause memory usage. Therefore, we need to manage this pointer. The disadvantage of the new object is that the lifecycle is too long and only explicit Delete can be used. Therefore, construct an object management pointer in the stack.
This object is a smart pointer. When the lifecycle of a smart pointer ends, the system automatically calls the destructor of the smart pointer and adds and releases the pointer to be managed. Two birds in one fell swoop. Very clever.
What's more clever:
For example
Sp <amessage> meta = parsebuffer-> Meta ();
Meta-> findint64 ("timeus", & timeus)
Meta is clearly an object. How can I use "->"
This is because the SP class reloads the "->" operator. The WP class does not have the '->' overload character, so the object cannot be operated.
Inline T * operator-> () const {return m_ptr ;}
Now, meta can be used as a normal pointer.
I still have a question: how can c ++ native not support smart pointers so well?
Discover SP charm
For more information about the memory release code, see
"Frameworks/native/libs/GUI/surfacetextureclient. cpp" 844 lines -- 79% --
Void surfacetextureclient: freeallbuffers (){
For (INT I = 0; I <num_buffer_slots; I ++ ){
Mslots [I]. Buffer = 0;
}
}
Buffer is SP type.
= 0: the reload character of the call is as follows:
Template <typename T>
Sp <t> & sp <t>: Operator = (T * Other)
{
If (other) Other-> incstrong (this );
If (m_ptr) m_ptr-> decstrong (this );
M_ptr = Other;
Return * this;
}
Other needs to enhance the reference, because another pointer points to the object.
M_ptr should be used to reduce the intensity of the application. Because the assigned SP does not point to the original object, the reference count must be reduced by one.
It's too clever to take zero into account.
Note that there are four implementations of the overload symbol "=.
It's not easy to understand something.