Wp Vs Sp


android smart pointer

In the Android source code, often see the form: sp <xxx>, wp <xxx> this type definition, this is actually the Android smart pointer. Smart Pointers in C + + is a concept-based reference counting, object to solve the problem of automatic release. In C + + programming, there are two very annoying problems: First, forget the release of the dynamic application objects resulting in memory leaks; Second, after the release of the object in one place, and is used elsewhere, causing memory access errors . Programmers often need to spend a lot of energy to carefully designed to avoid these problems. After using the smart pointers, dynamic memory applications will be automatically released (somewhat similar to Java’s garbage collection), no need to use delete to release the object, no need to consider whether an object has been released in other places, so that reduce a lot of programming work, and greatly improve the stability of the program.

Android smart pointer to the relevant source code in the following two files:

frameworks aseincludeutilsRefBase.h

frameworks aselibsutilsRefBase.cpp

Involved in the relationship between class and class as shown below:

Android defined two types of smart pointers, one is a strong pointer sp (strong pointer), one is weak pointers (weak pointer). In fact, strong and weak references as more appropriate. Strong pointer and a general sense the same concept of smart pointers, reference counting to record by the number of users in the use of an object, if all users have to give up references to the object, the object is automatically destroyed.

Weak pointer to an object, but the weak pointer object is only the address of record that can not be accessed through the weak pointer to the object that is not true to the calling object by mentally handicapped member functions or access object member variables. To access the weak pointer object, the weak need to first upgrade to a strong pointer to a pointer (wp classes provided by the promote () method). Weak pointer object is likely to be destroyed in other places, if the object has been destroyed, wp of promote () method will return null pointer, so you can avoid the wrong address to access the situation.

Is not it amazing? Weak pointer is how to do this for? In fact, the truth is not complicated, reason is that each can be referenced by the smart pointer object while being attached to another weakref_impl type of object, this object is responsible for recording the object of strong and weak pointer reference counting pointer reference count. This object is a smart pointer implementation for internal use, users do not see the smart pointer object. This operation is weak pointer object, the reference count only when strong and weak reference count is zero, this object will be destroyed.

Having said that principle, following the look in the end how to use smart pointers. Suppose there is now a class MyClass, if you want to use smart pointers to refer to this class of objects, then this class must meet the following two prerequisites:

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

(2) The class must define a virtual constructor, the constructor requires that it be defined as:

virtual ~ MyClass ();

Meet the above conditions can be defined smart pointer class, and define similar methods and common indicators. Such as ordinary pointer is defined as:

MyClass * p_obj;

Smart pointer is defined as:

sp <MyClass> p_obj;

Be careful not to define sp <MyClass> * p_obj. Easy for beginners to make this mistake, this is actually equivalent to the definition of a pointer. Although there is no problem in the syntax, but it is best never to use such a definition.

Defines a smart pointer variable, you can use it as like an ordinary pointer, including the assignment, access object members, as the return value as a function of the parameters. For example:

p_obj = new MyClass (); / / careful not to write p_obj = new sp <MyClass>

sp <MyClass> p_obj2 = p_obj;

p_obj-> func ();

p_obj = create_obj ();

some_func (p_obj);

Note Do not try to delete a smart pointer that delete p_obj. Do not worry about the destruction of objects, smart pointers, the most important function is to automatically destroy the object is no longer used. No need to use an object, assign the pointer to NULL directly to:

p_obj = NULL;

Above that are strong indicators, weak and strong pointer pointer definition method is similar, but not by a weak pointer to access the object’s members. Here is an example of weak pointers:

wp <MyClass> wp_obj = new MyClass ();

p_obj = wp_obj.promote (); / / upgrade to strong pointer. However, to use here, rather than ->, is really a pointer to the name of its negative ah

wp_obj = NULL;

Smart pointer to use it is very convenient, in general better to use smart pointers instead of normal pointers. But need to know a smart pointer is an object, rather than a real pointer, so its efficiency is far less than the normal pointer. So in sensitive areas of operational efficiency, better not to use smart pointers as well.


Each Ref object has a WeakRefImpl object that keeps two reference counters, one for weak references and one for strong references. Whenever the strong reference counter is incremented or decremented then the weak reference counter also gets incremented or decremented. But when you create a wp<> to some object only the object's weak reference counter is incremented. Therefore, managed objects live as long as there is some strong pointer referring to it.

However, the managed object's WeakRefImpl object may live longer than the object itself because it lives as long as there is some weak pointer referring to the object. If that is the case and the object itself is already destroyed the weak pointer's toStrongRef method returns NULL to indicate that the object is not available anymore.

The Ref base class is thread-safe without needing expensive locks. It completely synchronizes the access to the reference counters using atomic operations. The sp<> and wp<> template classes are not thread-safe. So always make sure you copy the strong and weak pointers when you share managed objects with different threads.


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License