In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to achieve strong pointers in the Android system". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to achieve strong pointers in the Android system.
Strong pointer and weak pointer basis
Smart pointers in android include lightweight pointers, strong pointers and weak pointers.
Strong pointer: it maintains the life cycle of an object mainly through strong reference counting.
Weak pointer: it maintains the lifecycle of the pointed object mainly through weak reference counting.
If strong or weak pointers are used in a class, then the class must inherit from the class RefBase, because strong and weak pointers provide reference counters that are implemented through the RefBase class.
Strong and weak pointers are more intimate than lightweight pointers, so they generally work with each other.
Analysis of strong pointer principle
The following analysis of the source code comes from the android5.0 system source code.
The definition and implementation of strong pointers are mainly in the file\ frameworks\ rs\ cpp\ util\ RefBase.h
Class RefBase {public: / / defines the reference count of member variables to maintain strongly referenced objects void incStrong (const void* id) const; / / defines member variables to maintain reference counts of strongly referenced objects void decStrong (const void* id) const; void forceIncStrong (const void* id) const / / gets the number of strong pointer counts. Int32_t getStrongCount () const; / / this class mainly implements class weakref_type {public: RefBase* refBase () const; void incWeak (const void* id); void decWeak (const void* id); / / acquires a strong reference if there is already one. Bool attemptIncStrong (const void* id); / / acquires a weak reference if there is already one. / / This is not always safe. See ProcessState.cpp and BpBinder.cpp / / for proper use. Bool attemptIncWeak (const void* id) / /! DEBUGGING ONLY: Get current weak ref count. Int32_t getWeakCount () const; / /! DEBUGGING ONLY: Print references held on object. Void printRefs () const; / /! DEBUGGING ONLY: Enable tracking for this object. / / enable-enable/disable tracking / / retain-when tracking is enable, if true, then we save a stack trace / / for each reference and dereference; when retain = = false, we / / match up references and dereferences and keep only the / / outstanding ones. Void trackMe (bool enable, bool retain);}; weakref_type* createWeak (const void* id) const; weakref_type* getWeakRefs () const; / /! DEBUGGING ONLY: Print references held on object. Inline void printRefs () const {getWeakRefs ()-> printRefs ();} /! DEBUGGING ONLY: Enable tracking of object. Inline void trackMe (bool enable, bool retain) {getWeakRefs ()-> trackMe (enable, retain);} typedef RefBase basetype;protected: RefBase (); virtual ~ RefBase (); / /! Flags for extendObjectLifetime () enum {OBJECT_LIFETIME_STRONG = 0x0000, OBJECT_LIFETIME_WEAK = 0x0001, OBJECT_LIFETIME_MASK = 0x0001}; void extendObjectLifetime (int32_t mode); / /! Flags for onIncStrongAttempted () enum {FIRST_INC_STRONG = 0x0001}; virtual void onFirstRef (); virtual void onLastStrongRef (const void* id); virtual bool onIncStrongAttempted (uint32_t flags, const void* id); virtual void onLastWeakRef (const void* id); private: friend class ReferenceMover Static void moveReferences (void* d, void const* s, size_t n, const ReferenceConverterBase& caster); private: friend class weakref_type; / / gets counter data through class objects. Class weakref_impl; RefBase (const RefBase& o); RefBase& operator= (const RefBase& o); weakref_impl* const mRefs;}
From the above class definition, you can see that there is a weakref_type class nested in the RefBase class, and this weakref_type class also has an object mRefs to describe the reference count of the object. That is, each RefBase object contains a weakref_type object.
Virtual represents a virtual function and friend represents a friend function.
At this point, I believe you have a deeper understanding of "how to achieve strong pointers in the Android system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.