Use MediaPlayer to illustrate SP implementations. SP is a template class, and T is a subclass of Refbase. As long as classes that inherit from Refbase can use the SP. Binder class also inherits Refbase class, the implementation of binder is inseparable from refbase.
Use the following code to talk about the doubts I encountered.
201 Static sp<mediaplayer> Getmediaplayer (jnienv* env, Jobject thiz)
202 {
203 Mutex::autolock L (slock);
204 mediaplayer* Const P = (mediaplayer*) Env->getintfield (Thiz, Fields.context);
205 return sp<mediaplayer> (p); (1) C + + Two types of initialization object methods.
206}
413 static void
414 Android_media_mediaplayer_start (jnienv *env, Jobject thiz)
415 {
416 ALOGV ("Start");
417 sp<mediaplayer> MP = Getmediaplayer (env, thiz); (2) Overloaded operator =
418 if (MP = = NULL) {
419 jnithrowexception (env, "java/lang/illegalstateexception", NULL);
420 return;
421}
422 Process_media_player_call (env, Thiz, Mp->start (), NULL, NULL); (3)
423}
What is an SP?
MP is the object of sp<mediaplayer>, how can I call the start function of MediaPlayer?
SP is a template class. :
How does the MP call Start ()?
To view the SP source class, you will find that the SP class has overridden->, so that it points to the member variable m_ptr (t* template parameter object, such as MediaPlayer),
The return value of mp-> is m_ptr, that is, the object returned is MediaPlayer, of course, the start method can be called.
In fact, the SP also rewrites the & and = operators.
The life cycle of the MP is only valid in the Android_media_mediaplayer_start method, which is released when the method is executed.
This is problematic, if all of the MP inside the memory is released, and other places there are references, there will be null pointers. If it is not released, other places are not referenced, the memory leaks.
How to solve this problem?
Android introduces a reference count, which holds the number of references in the Weakref_impl class and records how many times T is referenced. The number of citations is 0 o'clock on release. But as,sp<t1> and sp<t2> cross-references, they are interdependent and cannot be released. Strong and weak references are introduced.
SP WP Refbase the relationship between T classes such as
With sp<mediaplayer> analysis, counting process,
Return sp<mediaplayer> (P); When constructed, the M_ptr->incstrong (this) method is executed to add a strong and weak reference to the MediaPlayer object.
After Getmediaplayer executes, the destructor of the anonymous object is called ~SP, M_ptr->decstrong (this) is executed, reducing the strength and weakness of the MediaPlayer object by one.
In the Android_media_mediaplayer_start function, the MP is assigned, using the overloaded operator=, the overloaded process also executes M_ptr->incstrong (this), It also increases the strength reference of the MediaPlayer object by 1. When the Android_media_mediaplayer_start function finishes executing, releasing the MP will invoke the MP destructor, execute M_ptr->decstrong (this), and add one to the MediaPlayer object's strong and weak references.
Android Framework Review SP and WP SP objects