Android中的sp和wp指针

来源:互联网 发布:三国志乱舞软件 编辑:程序博客网 时间:2024/05/22 03:28

经常会在android的framework代码中发现sp<xxx>和wp<xxx>这样的指针,平时看的时候都把他当成一个普通的指针封装过掉了,这几天终于忍不住了,想深入了解一下。

相关的代码:

frameworks/base/include/utils/RefBase.h

frameworks/base/libs/utils/RefBase.cpp

sp和wp都是一个模板类,看一下sp类的定义:

 

view plaincopy to clipboardprint?
  1. template <typename T>   
  2. class sp   
  3. {   
  4. public:   
  5.     typedef typename RefBase::weakref_type weakref_type;   
  6.     inline sp() : m_ptr(0) { }   
  7.     sp(T* other);   
  8.     sp(const sp<T>& other);   
  9.     ~sp();   
  10.     ......   
  11. private:       
  12.     // Optimization for wp::promote().   
  13.     sp(T* p, weakref_type* refs);   
  14.        
  15.     T*              m_ptr;   
  16. };  

 

可以看到他确实封转了一个原生指针T* m_ptr. 再看一下其中一个构造函数和析构函数:

 

view plaincopy to clipboardprint?
  1. template<typename T>   
  2. sp<T>::sp(T* other)   
  3.     : m_ptr(other)   
  4. {   
  5.     if (other) other->incStrong(this);   
  6. }   
  7.   
  8. template<typename T>   
  9. sp<T>::~sp()   
  10. {   
  11.     if (m_ptr) m_ptr->decStrong(this);   
  12. }  

 

咋一看好奇怪,因为在构造函数中调用了incStrong(),在析构函数中调用的decStrong(),显然是管理引用计数的函数,但是sp类的中并没有定义这两个函数,这两个函数是在RefBase类中定义的,由此可以得出结论:

要想使用sp<T>或者wp<T>, T必需要继承RefBase类才行

RefBase的静态关系如下:

 

 其中weakref_type是RefBase的内嵌类,weakref_impl则是weakref_type的子类,RefBase的大部分工作都是交由weakref_impl类来完成,通过RefBase的成员变量weakref_impl* const mRefs。查看其中一个sp的构造函数:

 

view plaincopy to clipboardprint?
  1. template<typename T>   
  2. sp<T>::sp(T* other)   
  3.     : m_ptr(other)   
  4. {   
  5.     if (other) other->incStrong(this);   
  6. }  

 

建立sp<xxx>的动态关系如下:

sp<T>

  --> RefBase : incStrong()

  -->weakref_impl : addStrongRef()

  -->android_atomic_inc(&refs->mStrong)

可见当一个普通指针变成一个sp指针后,将会由RefBase类维护该指针的引用计数,当引用为零时则自动释放该指针指向的内存:

 

view plaincopy to clipboardprint?
  1. void RefBase::decStrong(const void* id) const  
  2. {   
  3.     weakref_impl* const refs = mRefs;   
  4.     refs->removeStrongRef(id);   
  5.     const int32_t c = android_atomic_dec(&refs->mStrong);   
  6.     if (c == 1) {   
  7.         const_cast<RefBase*>(this)->onLastStrongRef(id);   
  8.         if ((refs->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) {   
  9.             delete this;    //引用为0,销毁   
  10.         }   
  11.     }   
  12.     refs->removeWeakRef(id);   
  13.     refs->decWeak(id);   
  14. }  

 

 wp<xxx>是怎么一回事?

wp其实是弱指针的意思,wp<T>类型不能直接对类型T进行操作,要想对T进行某种操作,必需把wp升级为sp指针,使用promote()来实现升级:

        wp<T> weakp= new T();

        sp<T> t = weakp.promote();

wp可能会在弱引用计数不为0的情况下被销毁,执行如下代码:

 

view plaincopy to clipboardprint?
  1. class WPTest : public RefBase {   
  2. public:   
  3.     WPTest(){   
  4.         LOGD("WPTest constructor");   
  5.     }   
  6.     virtual ~WPTest() {   
  7.         LOGD("WPTest destructor");   
  8.     }   
  9.   
  10.     virtual void onFirstRef() {   
  11.         LOGD("first weak ptr ref callback");   
  12.     }   
  13.   
  14.     virtual void onLastStrongRef(const void* id) {   
  15.         LOGD("last strong ptr ref callback");   
  16.     }   
  17.   
  18.     virtual void onLastWeakRef(const void* id) {   
  19.         LOGD("last weak ptr ref callback");   
  20.     }   
  21. };   
  22.   
  23.   
  24. int main()   
  25. {   
  26.     WPTest *T = new WPTest();   
  27.     {   
  28.         wp<WPTest> weakp(T);   
  29.   
  30.         {   
  31.             LOGD("promote to strong ptr.../n");   
  32.   
  33.             sp<WPTest> strongp = weakp.promote();   
  34.   
  35.             LOGD("strong ptr's lifetime is just about to finish .../n");   
  36.         }   
  37.   
  38.         LOGD("weak ptr's lifetime is just about to finish .../n");   
  39.     }   
  40.   
  41.     LOGD("weak ptr is out of scope./n");   
  42.   
  43.     return 0;   
  44. }  

 

程序打印的结果是:

D/sp-wp-sample(  225): WPTest constructor
D/sp-wp-sample(  225): promote to strong ptr...
D/sp-wp-sample(  225): first weak ptr ref callback
D/sp-wp-sample(  225): strong ptr's lifetime is just about to finish ...
D/sp-wp-sample(  225): last strong ptr ref callback
D/sp-wp-sample(  225): WPTest destructor
D/sp-wp-sample(  225): weak ptr's lifetime is just about to finish ...
D/sp-wp-sample(  225): weak ptr is out of scope.

由此可见虽然wp<WPTest >的生命周期还没有结束,但是因为升级为sp<WPTest >后,sp<WPTest >的强引用计数为0,导致WPTest 被销毁,当强引用为0而弱引用不为0时,WPTest 销毁时,基类RefBase的mRefs指向的weakref_impl类并没有释放,从而保证了弱引用可以继续起作用,这点可以从RefBase的析构函数中看出来:

 

view plaincopy to clipboardprint?
  1. RefBase::~RefBase()   
  2. {   
  3. //    LOGV("Destroying RefBase %p (refs %p)/n", this, mRefs);   
  4.     if (mRefs->mWeak == 0) {   
  5. //        LOGV("Freeing refs %p of old RefBase %p/n", mRefs, this);   
  6.         delete mRefs;   
  7.     }   
  8. }  

 

不过也可以改变这一行为,我们修改一下WPTest的构造函数:

 

view plaincopy to clipboardprint?
  1. WPTest(){   
  2.         LOGD("WPTest constructor");   
  3.         extendObjectLifetime(OBJECT_LIFETIME_WEAK);   
  4.     }  

 

这时的打印结果是:

D/sp-wp-sample(  217): WPTest constructor
D/sp-wp-sample(  217): promote to strong ptr...
D/sp-wp-sample(  217): first weak ptr ref callbac
D/sp-wp-sample(  217): strong ptr's lifetime is j
D/sp-wp-sample(  217): last strong ptr ref callba
D/sp-wp-sample(  217): weak ptr's lifetime is j
D/sp-wp-sample(  217): last weak ptr ref callback
D/sp-wp-sample(  217): WPTest destructor
D/sp-wp-sample(  217): weak ptr is out of scope.

可以看出现在只有当强引用和弱引用的计数都为0时,WPTest对象才会被销毁。