Android framework review sp and wp sp objects, androidwp
Use MediaPlayer to describe the implementation of sp. Sp is a template class, and T is a subclass of RefBase. Sp can be used for any class inherited from RefBase. The binder class also inherits the RefBase class. The implementation of the binder is inseparable from the RefBase class.
Use the following code to explain my doubts.
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 object initialization 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 sp?
Mp is the object of sp <MediaPlayer>. How can I call the MediaPlayer start function?
Sp is a template class. :
Then how does mp call start?
View the sp source code class, and you will find that the sp class has been rewritten->, so that-> points to the member variable m_ptr (T * template parameter object, such as MediaPlayer ),
The returned value of mp-> is m_ptr, that is, the returned MediaPlayer object. Of course, you can call the start method.
In fact, the & and = operators are also rewritten in sp.
The life cycle of mp is only valid in the android_media_MediaPlayer_start method. After the method is executed, it should be released.
This is problematic. If all the memory in the mp is released and there are references elsewhere, a null pointer will appear. If you do not release the file and do not reference it elsewhere, the memory will be leaked.
How can this problem be solved?
Android introduces reference counting. The weakref_impl class stores the number of references and records the number of times T is referenced. Release when the number of references is 0. However, if sp <T1> and sp <T2> are referenced by each other, they are mutually dependent and cannot be released. Strong references and weak references are introduced.
The relationship between sp wp RefBase T classes is shown in figure
Using sp <MediaPlayer> for analysis and counting,
Return sp <MediaPlayer> (p); execute the m_ptr-> incStrong (this) method when constructing the MediaPlayer object, so that both strong and weak references of the MediaPlayer object are added,
After getMediaPlayer is executed, it calls the destructor of an anonymous object ~ Sp: Execute m_ptr-> decStrong (this) to reduce the strength and weakness of the MediaPlayer object by one.
In the android_media_MediaPlayer_start function, assign values to the mp using the overloaded operator =. The overloaded process also executes m_ptr-> incStrong (this ), it also increases the strength of the MediaPlayer object by 1. After the android_media_MediaPlayer_start function is executed, the mp will be released, the mp destructor will be called, m_ptr-> decStrong (this) will be executed, and the strong and weak references of the MediaPlayer object will be added. For example:
What are the differences between wp visa and sp visa?
Work permits in Singapore are classified into three types: EP, WP, and SP.
WP is a service license with a monthly salary of less than 1800. WP = work permit. The Chinese translation is a work permit.
SP is for a monthly salary of more than 1800, with a work permit applied for by the undergraduate or specialist. SP = special pass. The Chinese translation is a special criterion.
EP is divided into three types: 1. p, with a salary of more than January in 7000, has a recognized university degree or above, such as professional qualification and specialist skills, and works in management or proffessional, administrative, and executive jobs. (Senior management personnel and professional occupation) 2. p in February, the salary is 3500-7000, And the requirement is similar to that of p1 3. p, with a salary of more than March in 2500, the educational requirements are the same as those of p1. It is suitable for people with relevant work experience and technical skills.
EP = employee pass.
There is also a family name permit (DP), which is a dependent's pass in English.
What is the difference between netFramework-v35 and netFramework-v35 SP1?
SP1 is a release version after a patch package is installed on v3.5. You only need to install SP1.