292
查看Android中的sp和wp指针的源代码
Android中的sp和wp指针
0
←
Android中的sp和wp指针
跳转至:
导航
、
搜索
因为以下原因,你没有权限编辑本页:
你被禁止执行你刚才请求的操作。
您可以查看并复制此页面的源代码:
经常会在android的framework代码中发现sp<xxx>和wp<xxx>这样的指针,平时看的时候都把他当成一个普通的指针封装过掉了,这几天终于忍不住了,想深入了解一下。 相关的代码: frameworks\base\include\utils\RefBase.h frameworks\base\libs\utils\RefBase.cpp sp和wp都是一个模板类,看一下sp类的定义: <source lang="cpp"> 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; }; </source> 可以看到他确实封转了一个原生指针T* m_ptr. 再看一下其中一个构造函数和析构函数: <source lang="cpp"> 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); } </source> 咋一看好奇怪,因为在构造函数中调用了incStrong(),在析构函数中调用的decStrong(),显然是管理引用计数的函数,但是sp类的中并没有定义这两个函数,这两个函数是在RefBase类中定义的,由此可以得出结论: 要想使用sp<T>或者wp<T>, T必需要继承RefBase类才行。 RefBase的静态关系如下: [[image:Android_sp_wp_1.png]] 其中weakref_type是RefBase的内嵌类,weakref_impl则是weakref_type的子类,RefBase的大部分工作都是交由weakref_impl类来完成,通过RefBase的成员变量weakref_impl* const mRefs。查看其中一个sp的构造函数: <source lang="cpp"> 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); } </source> 建立sp<xxx>的动态关系如下: sp<T> :--> RefBase : incStrong() :-->weakref_impl : addStrongRef() :-->android_atomic_inc(&refs->mStrong) 可见当一个普通指针变成一个sp指针后,将会由RefBase类维护该指针的引用计数,当引用为零时则自动释放该指针指向的内存: <source lang="cpp"> 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); } </source> wp<xxx>是怎么一回事?<br> wp其实是弱指针的意思,wp<T>类型不能直接对类型T进行操作,要想对T进行某种操作,必需把wp升级为sp指针,使用promote()来实现升级: <source lang="cpp"> wp<T> weakp= new T(); sp<T> t = weakp.promote(); </source> wp可能会在弱引用计数不为0的情况下被销毁,执行如下代码: <source lang="cpp"> 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; } </source> 程序打印的结果是: D/sp-wp-sample( 225): WPTest constructor<br> D/sp-wp-sample( 225): promote to strong ptr...<br> D/sp-wp-sample( 225): first weak ptr ref callback<br> D/sp-wp-sample( 225): strong ptr's lifetime is just about to finish ...<br> D/sp-wp-sample( 225): last strong ptr ref callback<br> D/sp-wp-sample( 225): WPTest destructor<br> D/sp-wp-sample( 225): weak ptr's lifetime is just about to finish ...<br> D/sp-wp-sample( 225): weak ptr is out of scope.<br> 由此可见虽然wp<WPTest >的生命周期还没有结束,但是因为升级为sp<WPTest >后,sp<WPTest >的强引用计数为0,导致WPTest 被销毁,当强引用为0而弱引用不为0时,WPTest 销毁时,基类RefBase的mRefs指向的weakref_impl类并没有释放,从而保证了弱引用可以继续起作用,这点可以从RefBase的析构函数中看出来: <source lang="cpp"> 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; } } </source> 不过也可以改变这一行为,我们修改一下WPTest的构造函数: <source lang="cpp"> WPTest(){ LOGD("WPTest constructor"); extendObjectLifetime(OBJECT_LIFETIME_WEAK); } WPTest(){ LOGD("WPTest constructor"); extendObjectLifetime(OBJECT_LIFETIME_WEAK); } </source> 这时的打印结果是: D/sp-wp-sample( 217): WPTest constructor<br> D/sp-wp-sample( 217): promote to strong ptr...<br> D/sp-wp-sample( 217): first weak ptr ref callbac<br> D/sp-wp-sample( 217): strong ptr's lifetime is j<br> D/sp-wp-sample( 217): last strong ptr ref callba<br> D/sp-wp-sample( 217): weak ptr's lifetime is j<br> D/sp-wp-sample( 217): last weak ptr ref callback<br> D/sp-wp-sample( 217): WPTest destructor<br> D/sp-wp-sample( 217): weak ptr is out of scope.<br> 可以看出现在只有当强引用和弱引用的计数都为0时,WPTest对象才会被销毁。 原文地址 ---- http://blog.csdn.net/DroidPhone/archive/2010/08/09/5799792.aspx [[category:Android开发]] [[category:技术文章]]
返回
Android中的sp和wp指针
。
导航菜单
个人工具
   
个人维基
注册
登录
名字空间
页面
变换
查看
阅读
查看源代码
统计
查看历史
操作
搜索
导航
首页
最近更改
随机页面
工具箱
所有页面
文件列表
特殊页面