Upcoming SlideShare
×

# 03 strong weak_pointer

680 views
535 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
680
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 03 strong weak_pointer

1. 1. Drive for better visionsp: strong pointerwp: weak pointerFebruary, 2012 Himax Media Solutions 承景科技股份有限公司
2. 2. smart pointer  目的：物件建立時取得資源，物件消失時自動釋放資源，並避免 memory leak 的問題  範例  arrayA = new int[10]; arrayB = new int[10];  pa = arrayA; pb = arrayB;  pa = pb;  危險 ! arrayA 無法被釋放 !  希望達成的樣子  pa = pb;  此時 arrayA 會被立即釋放  pb = 0;  arrayB 會被立即釋放Himax Media Solutions Proprietary & Confidential 2
3. 3. strong and weak pointer  strong pointer  多個 strong pointer 可以指向同一個物件，但指向這個物件的 strong reference 為 0 時，會刪除物件  pa = pb;  物件的 ref+1  pb = 0;  物件的 ref-1  pa = 0;  物件的 ref=0 ，釋放  weak pointer  多個 weak pointer 可以指向同一個物件，即使 weak reference 不為 0 仍然可能刪除物件  pc = pd = pa;  pc,pd 都指向同一物件  pa = 0;  物件被釋放，但 pc,pd 仍然指向同一位置  RefBase  所有能被 strong/weak pointer 指向的物件，都必須繼承此 class  能自動記錄被參考的數量Himax Media Solutions Proprietary & Confidential 3
4. 4. 應用 : IPC Binder process A process BHimax Media Solutions Proprietary & Confidential 4
5. 5. RefBase::RefBase() RefBase mRefs weakref_impl mStrong = 0 bit28=1 mWeak = 0 mBase mFlags = 0  mStrong bit28==1 代表還沒有被參考過Himax Media Solutions Proprietary & Confidential 5
6. 6. RefBase::incStrong(id) sp<RefBase> RefBase mRefs onFirstRef() call weakref_impl mStrong +1 bit28=0 mWeak +1 mBase mFlags  id 會傳 this 或 clazz 指標，用來 debug  當第一次 mStrong+1 後， bit28 就被設為 0Himax Media Solutions Proprietary & Confidential 6
7. 7. RefBase::decStrong(id) sp<RefBase> RefBase mRefs onLastStrongRef() call weakref_impl mStrong -1 mWeak -1 mBase mFlags  當 mStrong/mWeak 為 0 時，會根據 life time 決定是否刪除物 件Himax Media Solutions Proprietary & Confidential 7
8. 8. RefBase::weakref_type::incWeak(id) wp<RefBase> RefBase mRefs weakref_impl mStrong mWeak +1 mBase mFlagsHimax Media Solutions Proprietary & Confidential 8
9. 9. RefBase::weakref_type::decWeak(id) wp<RefBase> RefBase mRefs weakref_impl mStrong mWeak -1 mBase mFlags  當 mWeak 為 0 時，會根據 life time 決定是否刪除物件Himax Media Solutions Proprietary & Confidential 9
10. 10. strong pointer  RefBase *r1=new RefBase(), *r2=new RefBase();  sp<RefBase> p1, p2; // 定義 strong pointer  p1 = r1; //r1->incStrong(), mStrong++, mWeak++  p2 = r1; //r2->incStrong(), mStrong++, mWeak++  p1 = r2; //r1->decStrong(), r2->incStrong()  p2 = NULL; //r1->decStrong() 導致 r1 被刪除  p1 = NULL; //r2->decStrong() 導致 r2 被刪除Himax Media Solutions Proprietary & Confidential 10
11. 11. weak pointer  RefBase *r1=new RefBase(), *r2=new RefBase();  wp<RefBase> p1, p2; // 定義 weak pointer  p1 = r1; //r1->incWeak()  p2 = r1; //r1->incWeak()  p1 = r2; //r1->decWeak(), r2->incWeak()  p2 = NULL; //r1->decWeak()  p1 = NULL; //r2->decWeak()Himax Media Solutions Proprietary & Confidential 11
12. 12. Life Time  mFlags==0 (strong life time)  mStrong=0 時，刪除 RefBase 物件，保留 weakref_impl 物件  mWeak=0 時 ( 表示 mStrong=0) ，刪除 weakref_impl 物件  mFlags==1 (weak life time)  當 mStrong=0 時，不作任何動作  當 mWeak=0 時，呼叫 onLastWeakRef(id) ，刪除 RefBase 物件， 保留 weakref_impl 物件Himax Media Solutions Proprietary & Confidential 12
13. 13. LIFETIME_STRONG: mStrong = 0, mWeak != 0 RefBase wp<RefBase> mRefs weakref_impl mStrong -1 mWeak -1 mBase mFlags  刪除 strong pointer ，導致 mStrong 為 0 ，但仍有 weak pointer 存在Himax Media Solutions Proprietary & Confidential 13
14. 14. LIFETIME_STRONG: mStrong = 0, mWeak = 0 RefBase mRefs weakref_impl mStrong mWeak -1 mBase mFlags  刪除最後一個 weak pointerHimax Media Solutions Proprietary & Confidential 14
15. 15. LIFETIME_STRONG: mStrong != 0, mWeak != 0 sp<RefBase> RefBase mRefs weakref_impl mStrong mWeak -1 mBase mFlags  刪除 weak pointer ，但還有 strong pointer 存在Himax Media Solutions Proprietary & Confidential 15
16. 16. LIFETIME_STRONG: mStrong = 0, mWeak = 0 RefBase mRefs weakref_impl mStrong -1 mWeak -1 mBase mFlags  刪除最後一個 strong pointerHimax Media Solutions Proprietary & Confidential 16
17. 17. RefBase::extendObjectLifetime(mode)  extendObjectLifetime (OBJECT_LIFETIME_WEAK)  將 strong lifetime 改成 weak life time  會出現在繼承 RefBase 的 class 中，而且通常是 constructor 就決定是 weak lifetimeHimax Media Solutions Proprietary & Confidential 17
18. 18. LIFETIME_WEAK: mStrong = 0, mWeak != 0 RefBase wp<RefBase> mRefs weakref_impl mStrong -1 mWeak -1 mBase mFlags  刪除 strong pointer ，導致 mStrong 為 0 ，但仍有 weak pointer 存在  會保留 RefBase 物件Himax Media Solutions Proprietary & Confidential 18
19. 19. LIFETIME_WEAK: mStrong = 0, mWeak = 0 RefBase mRefs call onLastWeakRef() weakref_impl mStrong mWeak -1 mBase mFlags  刪除最後一個 weak pointerHimax Media Solutions Proprietary & Confidential 19
20. 20. LIFETIME_WEAK: mStrong != 0, mWeak != 0 sp<RefBase> RefBase mRefs weakref_impl mStrong mWeak -1 mBase mFlags  刪除 weak pointer ，但還有 strong pointer 存在Himax Media Solutions Proprietary & Confidential 20
21. 21. LIFETIME_WEAK: mStrong = 0, mWeak = 0 RefBase mRefs call onLastWeakRef() weakref_impl mStrong -1 mWeak -1 mBase mFlags  刪除最後一個 strong pointer  和 mWeak=0 的效果相同Himax Media Solutions Proprietary & Confidential 21
22. 22. wp<T>::promote() sp<RefBase> strong lifetime weak lifetime RefBase wp<RefBase> promote( mRefs ) weakref_impl mStrong +1 mWeak +1 mBase mFlags call attempIncStrong()Himax Media Solutions Proprietary & Confidential 22
23. 23. LightRefBase  只支援 strong pointer 的 RefBase  incStrong()  decStrong() LightRefBase mStrongHimax Media Solutions Proprietary & Confidential 23
24. 24. Drive for better vision