Android smart pointer usage

Source: Internet
Author: User

The Android mobile operating system is an open-source operating system. The open source code of various functions is stored in a specific folder. We can modify the source code to easily complete the required functions. Here we will take a look at the Android smart pointer source code interpretation and application methods.

  • Introduction to the callback method of the Android event listener
  • Modify the Android simulator size at will
  • Comprehensive Explanation of Android Multimedia Framework source code
  • How to correctly use the Android command line Startup Program
  • How to query contact information in Android

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: one is to forget to release the dynamically applied object, causing memory leakage; the other is to release the object in one place, it is used elsewhere, causing memory access errors.

Programmers often need to spend a lot of energy designing carefully to avoid these problems. After the smart pointer is used, the dynamically applied memory will be automatically released, which is a bit similar to Java garbage collection.) You do not 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.

The source code of Android smart pointer is included 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 spstrong pointer, and the other is 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 member functions of the object or the member variables of the object cannot be called by being mentally retarded. To access the objects pointed to by the weak pointer, you must first upgrade the weak pointer to the promote () method provided by the strong pointer through 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.

Is it amazing? 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 the Android smart pointer. the user of the smart pointer 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.

After talking about so many principles, let's see how to use smart pointers. Suppose there is a class MyClass. If you want to use a smart pointer to reference the object of this class, the class must meet the following two prerequisites:

1) This class is a subclass or indirect subclass of the base class RefBase;

2) this class must define a virtual constructor, that is, its constructor needs to be defined as follows:

 
 
  1. virtual ~MyClass(); 

Classes that meet the preceding conditions can define Android smart pointers, which are similar to common pointers. For example, a normal pointer is defined as follows:

 
 
  1. MyClass* p_obj; 

Android smart pointers are defined as follows:

 
 
  1. sp< MyClass> p_obj; 

Do not define it as sp <MyClass> * p_obj. It is easy for beginners to make this mistake, which is actually equivalent to defining a pointer. Although there is no syntax problem, it is best not to use such a definition.

A variable that defines a smart pointer can be used as a normal pointer, including value assignment, access to object members, return values as functions, and parameters as functions. For example:

 
 
  1. P_obj = new MyClass ();
  2. // Do not write p_obj = new sp <MyClass>
  3. Sp <MyClass> p_objp_obj2 = p_obj;
  4. P_obj-> func ();
  5. P_obj = create_obj ();
  6. Some_func (p_obj );

Do not try to delete an Android smart pointer, that is, delete p_obj. Do not worry about object destruction. Smart pointers are used to automatically destroy objects that are no longer in use. If you do not need to use another object, you can directly assign a NULL value to the pointer:

 
 
  1. p_obj = NULL; 

All of the above are strong pointers. The definition of weak pointers is similar to that of strong pointers, but they cannot be used to access object members. The following is an example of a weak pointer:

 
 
  1. Wp <MyClass> wp_obj = new MyClass ();
  2. P_obj = wp_obj.promote ();
  3. // Upgrade to a strong pointer. But here we need to use. Instead of->. It's really a negative pointer.
  4. Wp_obj = NULL;

Android smart pointers are very convenient to use. Generally, it is best to use smart pointers instead of normal pointers. However, you need to know that a smart pointer is actually an object rather than a real pointer, so its running efficiency is far inferior to that of a common pointer. Therefore, we recommend that you do not use smart pointers when you are sensitive to operation efficiency.

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.