SP and WP pointer in Android

Source: Internet
Author: User

In the source code of Android, you will often see definitions of types such as sp <XXX> and WP <XXX>, which are actually smart pointers in Android. Smart pointer is a concept in C ++. It solves the automatic release of objects by reference counting.

In C ++ programming, there are two headaches:

First, the system forgets to release the dynamic application object, causing memory leakage;

Second, after the object is released in one place, it is used elsewhere, causing memory access errors.

When the smart pointer is used, the dynamically applied memory will be automatically released (a bit similar to Java garbage collection), and no need to use Delete to release objects, you do not need to consider whether an object has been released elsewhere, which reduces programming workload and greatly improves program stability.

Android defines two types of smart pointers: strong pointer Sp (strong pointer) and weak pointer (weak pointer ). In fact, it is more suitable for strong references and weak references. Strong pointers share the same concept as smart pointers in general sense. The reference count is used to record how many users are using an object. If all users give up referencing this object, the object will be automatically destroyed. The weak pointer also points to an object, but the weak pointer only records the address of the object and cannot access the object through the weak pointer, that is to say, the weak pointer cannot be used to call the object's member functions or access the object's member variables. To access the objects pointed to by a weak pointer, you must first upgrade the weak pointer to a strong pointer (through the promote () method provided by the WP class ), the weak Pointer Points to an object that may be destroyed elsewhere. If the object has been destroyed, the WP promote () method returns a null pointer, in this way, address access errors can be avoided.

How does a weak pointer achieve this? In fact, this is not complicated because every object that can be referenced by a smart pointer is appended with another weakref_impl object, this object records the strong pointer reference count and weak pointer reference count of the object. This object is used internally by the implementation of smart pointers, and users of smart pointers cannot see this object. The weak pointer operation is this object. this object will be destroyed only when both the strong reference count and the weak reference count are 0.

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.