Android Refbase, SP, WP

Source: Internet
Author: User

First refbase in the C + + part of Android is as a base class for all classes, which functions like an object in Java. A private member exists in this class:weakref_impl* const mrefs;(Weakref_impl is a subclass of Weakref_type)This mrefs is a "shadow object" and is the key to managing the reference count of an objectRefbase is constructed in Mrefs (new Weakref_impl (This)), new is a Weakref_impl object, Weakref_impl's constructor parameter is Refbase's pointer and initializes the WEAKREF_IMPLMstrongAndMweakThese two members, both of which are key strong reference counts and weak reference counts/********************refbase use and principles are actually very simple *****************************/take a look at SP and WP ( 4.4 and later version of the source, SP is not in the refbase, is placed in the Strongpointer file) wp:template<typename t>wp<t>::wp (t* Other): M_ptr ( Other)//wp<a> uses m_ptr to point to the actual object {if (other) M_refs = Other->createweak (this);//m_ref is refbase in weakref_ The object of type, which can be seen asThe shadow object of the actual objectafter processingTo the members of WP, that is, WP holds a real object of the Shadow object}WP is a template class,wp<a> WpA (PA); Of course, many of its constructors can also be constructed with sp<a>.Other->createweak (this) will be transferred to Refbase's createweak and then called by the Shadow object Mrefs to increase the weak reference count WP's destructor: Template<typename t>wp<t &GT;::~WP () {if (m_ptr) m_refs->decweak (this);} In the process of WP destructor, go back to call the actual object of the shadow object of the weak reference, self-subtraction, the weak reference will determine the current reference count, if it is 0, call its own destruction, free memory SP is similar to WP but inside the SP there is only a member of a pointer to the actual object (but a pointer to the actual object can also find the Shadow object), the shadow object that will be found in the actual object during construction. Then, both the strong and weak references of the shadow object are added and then the value is divided into two steps: 1. First reduce the strong reference count of the Shadow object, and then judge if the strong reference count is 0, then the actual data itself is 2. Reduce the weak reference count of the Shadow object, and then release the Shadow object when the weak reference count is 0 This weak pointer and strong pointer play a role in the Android system:is to use this instead of the traditional pointer, let the code in the self-management of the object's memory release, to avoid manual memory leaksProject used PA = new A ();sp<a> SpA = PA;sp<a> SpA (PA);the equals operator is overloaded here.The overloaded operators are intended to make these operators more suitable for more class typesTemplate<typename t>sp<t>& sp<t>::operator = (t* other) {if [other] Other->incstrong (this); The meaning of this is an implicit parameter to the function in the class, pointing to the class itself, which is pointing to sp<t> if (m_ptr) M_ptr->decstrong (this);    Empty construction mptr Initialization 0 m_ptr = other; return *this;}

Android Refbase, SP, WP

Related Article

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.