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!