Android smart pointer Process

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.