admin管理员组

文章数量:1666599

几个相关class的定义位置:

system/core$ vim include/utils/RefBase.h 其中定义了class wp
system/core/libutils/RefBase.cpp 其中定义了class RefBase::weakref_impl

system/core$ vim include/utils/StrongPointer.h 其中定义了class sp

以以下代码运行为例:

链接:http://blog.csdn/mfbao01/article/details/6255655

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;
}

第一步:调用wp构造方法

system/core/include/utils/RefBase.h

template<typename T> template<typename U>
wp<T>::wp(U* other)
    : m_ptr(other)
{
    if (other) m_refs = other->createWeak(this);
}
m_refs为weakref_type类型的指针

上一步调了实例对像W

本文标签: 流程wpandroidSPPromote