Android Smart Pointer sp WP how to use the introduction

Source: Internet
Author: User

Android mobile OS is an open source operating system. Then in the specific folder will be stored in a variety of related functions of open source code. We can easily complete the functions we need when we use these source code to make corresponding changes. Here, let's take a look at Android intelligence
Pointers to the relevant source code interpretation and Application methods.

In the Android source code, often see shape like:sp< xxx>, wp< xxx> Such type definition, this is actually the smart pointer in Android. Smart pointers are a concept in C + + that solves the problem of automatic release of objects through a method based on reference counting. In C + + programming, there are two people who have a headache
Problem: One is to forget to release the object of the dynamic application to cause a memory leak, and the second is that the object is released in one place and used elsewhere, causing memory access errors.

Programmers often have to spend a lot of time designing carefully to avoid these problems. After using the smart pointer, the dynamically requested memory will be automatically freed (a bit like Java garbage collection), do not need to use Delete to release the object, do not need to consider whether an object has been elsewhere
Release, so that the program writing work to alleviate a lot, and the stability of the program greatly improved.

Android smart pointer related source code in the following two files:


Frameworks\base\include\utils\refbase.h

Frameworks\base\libs\utils\refbase.cpp

Two smart pointer types are defined in Android, one is a strong pointer sp (strong pointer) and one is a weak pointer (weak pointer). It is more appropriate to be a strong reference and a weak reference. A strong pointer is the same concept as a general-purpose smart pointer, and a reference count is used to record how many users are using a
Object, the object is automatically destroyed if all the users have discarded a reference to the object.

The weak pointer also points to an object, but the weak pointer simply records the address of the object, cannot access the object through a weak pointer, that is, the member function of the object cannot be invoked through the mentally retarded or access the member variables of the object. To access the object to which the weak pointer is pointing, you first upgrade the weak pointer to a strong pointer (by
WP class provides the promote () method). The object pointed to by the weak pointer is likely to be destroyed elsewhere, and if the object has been destroyed, the promote () method of WP will return a null pointer, thus avoiding the occurrence of an address access error.

Isn't it amazing? How does a weak pointer do this? In fact, it's not complicated at all, because every object that can be referenced by a smart pointer is appended with an object of another Weakref_impl type, which is responsible for documenting the object's strong pointer reference count and weak pointer reference count.
。 This object is used internally by the Android smart pointer implementation, and the user of the smart pointer does not see this object. The weak pointer operates on this object, which is destroyed only if the strong reference count and the weak reference count are 0 o'clock.

Having said so many principles, it's time to see how smart pointers work. Suppose you now have a class MyClass, if you want to use smart pointers to refer to objects of this class, then this class needs to meet the following two prerequisites:

(1) This class is a subclass or an indirect subclass of a base class refbase;

(2) This class must define a fictional constructor, that is, its constructor needs to be defined like this:

Virtual ~myclass ();
Classes that meet the above criteria can define Android smart pointers, which are similar to normal pointers. For example, the normal pointer is this definition:

Myclass* P_obj;
The Android smart pointer is defined like this:

sp< myclass> p_obj;
Be careful not to define sp< myclass>* p_obj. Beginners are prone to make this mistake, which is actually equivalent to defining a pointer pointer. Although there is no grammatical problem, it is best to never use such a definition.

A variable that defines a smart pointer can be used like a normal pointer, including assignment, access to an object member, as a function's return value, as a function parameter, and so on. Like what:

P_obj = new MyClass ();
Be careful not to write p_obj = new sp< myclass>
sp< myclass> p_objp_obj2 = p_obj;
P_obj->func ();
P_obj = Create_obj ();
Some_func (P_obj);
Be careful not to try to delete an Android smart pointer, that is, delete p_obj. Do not worry about object destruction, the most important role of smart pointers is to automatically destroy objects that are no longer used. After you do not need to use an object, assign the pointer to null directly:

P_obj = NULL;
These are strong pointers, and weak pointers are similar to strong pointers, but you cannot access the members of an object through weak pointers. The following is an example of a weak pointer:

wp< myclass> wp_obj = new MyClass ();
P_obj = Wp_obj.promote ();
Upgrade to a strong pointer. But it has to be used here. Not-> it's the name of the pointer.
Wp_obj = NULL;
Android smart pointers are handy, and in general it's best to use smart pointers instead of normal pointers. But it is necessary to know that a smart pointer is actually an object, not a real pointer, so its efficiency is far less than that of a normal pointer. So in a place that is sensitive to operational efficiency, it is best
Or do not use smart pointers as well

Android Smart Pointer sp WP how to use the introduction

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.