Introduction to Android reference count (2)

Source: Internet
Author: User

Talking about the principle of LightRefBase in implementing object count management in Android reference count 1), this article will analyze the heavyweight reference base class: RefBase implementation and its role.

The following is a class diagram of RefBase and related classes:

650) this. width = 650; "border =" 0 "width =" 683 "height =" 460 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/0F0321136-0.jpg "/>

As shown in the figure, the RefBase class has a member variable mRefs pointing to a weakref_impl object. This object manages the lifecycle of the RefBase. The weakref_impl class has two reference counts: one for the number of strong references and the other for the number of weak references.

There are two methods to control the lifecycle of RefBase:

1. Strong reference control: the mFlag value in the weakref_impl class is OBJECT_LIFETIME_STRONG;

2. Weak reference control: the mFlag value in the weakref_impl class is OBJECT_LIFETIME_WEAK;

When the lifecycle control mode is 1, when the strong reference count in weakref_impl is 0, the RefBase object will be destroyed, but the weakref_impl object will not be destroyed.

When the lifecycle control mode is 2, RefBase will be destroyed and weakref_impl will be destroyed only when both strong and weak references are 0.

If you want to manage reference counts for a defined class, you must inherit RefBase. The default lifecycle Control Method of RefBase is under strong reference control. When using the m_ptr pointer saved in wp, check whether the pointer is valid. Call the promote function to check whether the returned sp is null. If it is null, The RefBase has been destroyed.

 

This article is from the "HelloWord" blog and will not be reproduced!

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.