Android sp和wp指针

经常会在androidframework代码中发现sp<xxx>wp<xxx>这样的指针,平时看的时候都把他当成一个普通的指针封装过掉了,我们现在就来深入研究一下,我们要知道,spwp斗是一个模板类,那么我们就先来看看sp类的定义,这样有助于我们理解sp的含义。

Java代码:

  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. // 优化

  13. sp(T* p, weakref_type* refs);

  14. T* m_ptr;
  15. };
复制代码
       可以看到他确实封转了一个原生指针 T* m_ptr. 再看一下其中一个构造函数和析构函数:

Java代码:
  1. template<typename T>
  2. sp<T>::sp(T* other)
  3. : m_ptr(other)
  4. {
  5. if (other) other->incStrong(this);
  6. }

  7. template<typename T>
  8. sp<T>::~sp()
  9. {
  10. if (m_ptr) m_ptr->decStrong(this);
  11. }
复制代码

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

       要想使用 sp或者 wp, T必需要继承 RefBase类才行。那么我们来看看效果中给我们呈现的关系。

效果图:


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

Java代码:
  1. template<typename T>
  2. sp<T>::sp(T* other)
  3. : m_ptr(other)
  4. {
  5. if (other) other->incStrong(this);
  6. }
复制代码

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

Java代码:
  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是怎么一回事?

         wp其实是弱指针的意思, wp类型不能直接对类型T进行操作,要想对T进行某种操作,必需把wp升级为sp指针,使用 promote()来实现升级 :wp weakp= new T();sp t = weakp.promote();wp可能会在弱引用计数不为0的情况下被销毁,执行如下代码:

Java代码:
  1. class WPTest : public RefBase {
  2. public:
  3. WPTest(){
  4. LOGD("WPTest constructor");
  5. }
  6. virtual ~WPTest() {
  7. LOGD("WPTest destructor");
  8. }

  9. virtual void onFirstRef() {
  10. LOGD("first weak ptr ref callback");
  11. }

  12. virtual void onLastStrongRef(const void* id) {
  13. LOGD("last strong ptr ref callback");
  14. }

  15. virtual void onLastWeakRef(const void* id) {
  16. LOGD("last weak ptr ref callback");
  17. }
  18. };


  19. int main()
  20. {
  21. WPTest *T = new WPTest();
  22. {
  23. wp<WPTest> weakp(T);

  24. {
  25. LOGD("promote to strong ptr...\n");

  26. sp<WPTest> strongp = weakp.promote();

  27. LOGD("strong ptr's lifetime is just about to finish ...\n");
  28. }

  29. LOGD("weak ptr's lifetime is just about to finish ...\n");
  30. }

  31. LOGD("weak ptr is out of scope.\n");

  32. return 0;
  33. }
复制代码

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

Java代码:
  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的构造函数:

Java代码:
  1. WPTest(){
  2. LOGD("WPTest constructor");
  3. extendObjectLifetime(OBJECT_LIFETIME_WEAK);
  4. }
复制代码

       这时的打印结果是:

Java代码:
  1. D/sp-wp-sample( 217): WPTest constructor
  2. D/sp-wp-sample( 217): promote to strong ptr…
  3. D/sp-wp-sample( 217): first weak ptr ref callbac
  4. D/sp-wp-sample( 217): strong ptr’s lifetime is j
  5. D/sp-wp-sample( 217): last strong ptr ref callba
  6. D/sp-wp-sample( 217): weak ptr’s lifetime is j
  7. D/sp-wp-sample( 217): last weak ptr ref callback
  8. D/sp-wp-sample( 217): WPTest destructor
  9. D/sp-wp-sample( 217): weak ptr is out of scope.

复制代码

         可以看出现在只有当强引用和弱引用的计数都为0时, WPTest对象才会被销毁。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值