In AndroidSource code, You will often see the type definitions such as sp <XXX> and WP <XXX>, which are actually android
. The smart pointer is C ++.
To solve the automatic release of objects by reference counting. In C ++
Editing
There are two headaches in the process: first, forgetting to release dynamic applications
The requested object causes memory leakage. Second, the object is used elsewhere after it is released, causing memory access errors.ProgramStaff often need to spend a lot of energy on careful design to avoid these
Problem. When a smart pointer is used, the dynamically applied memory will be automatically released (similar to Java
Garbage collection), no need to use
Delete to release objects, and you do not need to consider whether a pair of objects has been released elsewhere. This reduces programming workload and greatly improves program stability.
Android smart pointer-related sourcesCodeIn the following two files:
Frameworks/base/include/utils/refbase. h
Frameworks/base/libs/utils/refbase. cpp
The relationships between classes and classes are shown in:
Android defines two smart pointer types: strong pointer Sp (strong pointer) and weak pointer (weak ).
Pointer ). In fact, strong references and weak references are more suitable. 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
If the user does not reference the 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, it cannot call the member function or access object of the object through the mental retardation.
Member changes
Quantity. 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 ). Objects pointed to by weak pointers may be sold elsewhere.
If the object has been destroyed, the promote () method of WP will return a null pointer to avoid address access errors.
Is it amazing? How does a weak pointer achieve this? The reason is that every object that can be referenced by a smart pointer is appended with another object.
Weakref_impl type object, which records the strong pointer reference count and weak pointer reference count of the object. This object is used internally by the smart pointer implementation.
You 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, then this class needs
The following two prerequisites are met:
(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:
Virtual ~ Myclass ();
A class that meets the preceding conditions can define smart pointers, which are similar to common pointers. For example, a normal pointer is defined as follows:
Myclass * p_obj;
The smart pointer is defined as follows:
Sp <myclass> p_obj;
Be sure not to 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:
P_obj = new myclass (); // do not write p_obj = new sp <myclass>
Sp <myclass> p_obj2 = p_obj;
P_obj-> func ();
P_obj = create_obj ();
Some_func (p_obj );
Do not try to delete a 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:
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:
WP <myclass> wp_obj = new myclass ();
P_obj = wp_obj.promote (); // upgrade to a strong pointer. But here we need to use. Instead of->. It's really a negative pointer.
Wp_obj = NULL;
It is very convenient to use smart pointers. 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, not a real pointer. Therefore
Its running efficiency is far inferior to that of common pointers. Therefore, we recommend that you do not use smart pointers when you are sensitive to operation efficiency.