Android framework review sp and wp sp objects

Source: Internet
Author: User

Android framework review sp and wp sp objects
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 GetMediaPlayer (JNIEnv * env, jobject thiz)
202 {
203 Mutex: Autolock l (sLock );
204 MediaPlayer * const p = (MediaPlayer *) env-> GetIntField (thiz, fields. context );
205 return sp (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 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 sp 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. But for example, sp And sp Mutual reference between them, so 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



Use sp Analysis, counting process,
Return sp (P); during construction, execute the m_ptr-> incStrong (this) method to add one to each weak reference of the MediaPlayer object,

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. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel/K/cDvw + ajrLbUbXC9 + kernel/K/kernel/O1xMe/yPXS/kernel + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20141020/20141020084540100.png" alt = "">

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.