Android中的sp和wp指针
经常会在android的framework代码中发现sp<xxx>和wp<xxx>这样的指针,平时看的时候都把他当成一个普通的指针封装过掉了,这几天终于忍不住了,想深入了解一下。
相关的代码:
frameworks\base\include\utils\RefBase.h
frameworks\base\libs\utils\RefBase.cpp
sp和wp都是一个模板类,看一下sp类的定义:
template <typename T> class sp { public: typedef typename RefBase::weakref_type weakref_type; inline sp() : m_ptr(0) { } sp(T* other); sp(const sp<T>& other); ~sp(); ...... private: // Optimization for wp::promote(). sp(T* p, weakref_type* refs); T* m_ptr; }; template <typename T> class sp { public: typedef typename RefBase::weakref_type weakref_type; inline sp() : m_ptr(0) { } sp(T* other); sp(const sp<T>& other); ~sp(); ...... private: // Optimization for wp::promote(). sp(T* p, weakref_type* refs); T* m_ptr; };
可以看到他确实封转了一个原生指针T* m_ptr. 再看一下其中一个构造函数和析构函数:
template<typename T> sp<T>::sp(T* other) : m_ptr(other) { if (other) other->incStrong(this); } template<typename T> sp<T>::~sp() { if (m_ptr) m_ptr->decStrong(this); } template<typename T> sp<T>::sp(T* other) : m_ptr(other) { if (other) other->incStrong(this); } template<typename T> sp<T>::~sp() { if (m_ptr) m_ptr->decStrong(this); }
咋一看好奇怪,因为在构造函数中调用了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的构造函数:
template<typename T> sp<T>::sp(T* other) : m_ptr(other) { if (other) other->incStrong(this); } template<typename T> sp<T>::sp(T* other) : m_ptr(other) { if (other) other->incStrong(this); }
建立sp<xxx>的动态关系如下:
sp<T>
- --> RefBase : incStrong()
- -->weakref_impl : addStrongRef()
- -->android_atomic_inc(&refs->mStrong)
可见当一个普通指针变成一个sp指针后,将会由RefBase类维护该指针的引用计数,当引用为零时则自动释放该指针指向的内存:
void RefBase::decStrong(const void* id) const { weakref_impl* const refs = mRefs; refs->removeStrongRef(id); const int32_t c = android_atomic_dec(&refs->mStrong); if (c == 1) { const_cast<RefBase*>(this)->onLastStrongRef(id); if ((refs->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) { delete this; //引用为0,销毁 } } refs->removeWeakRef(id); refs->decWeak(id); } void RefBase::decStrong(const void* id) const { weakref_impl* const refs = mRefs; refs->removeStrongRef(id); const int32_t c = android_atomic_dec(&refs->mStrong); if (c == 1) { const_cast<RefBase*>(this)->onLastStrongRef(id); if ((refs->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) { delete this; //引用为0,销毁 } } refs->removeWeakRef(id); refs->decWeak(id); }
wp<xxx>是怎么一回事?
wp其实是弱指针的意思,wp<T>类型不能直接对类型T进行操作,要想对T进行某种操作,必需把wp升级为sp指针,使用promote()来实现升级:
wp<T> weakp= new T(); sp<T> t = weakp.promote();
wp可能会在弱引用计数不为0的情况下被销毁,执行如下代码:
class WPTest : public RefBase { public: WPTest(){ LOGD("WPTest constructor"); } virtual ~WPTest() { LOGD("WPTest destructor"); } virtual void onFirstRef() { LOGD("first weak ptr ref callback"); } virtual void onLastStrongRef(const void* id) { LOGD("last strong ptr ref callback"); } virtual void onLastWeakRef(const void* id) { LOGD("last weak ptr ref callback"); } }; int main() { WPTest *T = new WPTest(); { wp<WPTest> weakp(T); { LOGD("promote to strong ptr...\n"); sp<WPTest> strongp = weakp.promote(); LOGD("strong ptr's lifetime is just about to finish ...\n"); } LOGD("weak ptr's lifetime is just about to finish ...\n"); } LOGD("weak ptr is out of scope.\n"); return 0; } class WPTest : public RefBase { public: WPTest(){ LOGD("WPTest constructor"); } virtual ~WPTest() { LOGD("WPTest destructor"); } virtual void onFirstRef() { LOGD("first weak ptr ref callback"); } virtual void onLastStrongRef(const void* id) { LOGD("last strong ptr ref callback"); } virtual void onLastWeakRef(const void* id) { LOGD("last weak ptr ref callback"); } }; int main() { WPTest *T = new WPTest(); { wp<WPTest> weakp(T); { LOGD("promote to strong ptr...\n"); sp<WPTest> strongp = weakp.promote(); LOGD("strong ptr's lifetime is just about to finish ...\n"); } LOGD("weak ptr's lifetime is just about to finish ...\n"); } LOGD("weak ptr is out of scope.\n"); return 0; }
程序打印的结果是:
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的析构函数中看出来:
RefBase::~RefBase() { // LOGV("Destroying RefBase %p (refs %p)\n", this, mRefs); if (mRefs->mWeak == 0) { // LOGV("Freeing refs %p of old RefBase %p\n", mRefs, this); delete mRefs; } } RefBase::~RefBase() { // LOGV("Destroying RefBase %p (refs %p)\n", this, mRefs); if (mRefs->mWeak == 0) { // LOGV("Freeing refs %p of old RefBase %p\n", mRefs, this); delete mRefs; } }
不过也可以改变这一行为,我们修改一下WPTest的构造函数:
WPTest(){ LOGD("WPTest constructor"); extendObjectLifetime(OBJECT_LIFETIME_WEAK); } WPTest(){ LOGD("WPTest constructor"); extendObjectLifetime(OBJECT_LIFETIME_WEAK); }
这时的打印结果是:
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对象才会被销毁。
原文地址
http://blog.csdn.net/DroidPhone/archive/2010/08/09/5799792.aspx