开源Platinum库里的智能指针机制

来源:互联网 发布:怎么投诉淘宝网 编辑:程序博客网 时间:2024/05/29 07:03

在研究 Platinum 的 dlna, 其中实现的操作 g_dlna_ctrl_point = (PLT_CtrlPointReference)new PLT_CtrlPoint(); 但是从来不做 delete 操作

当时一直很奇怪,后面仔细看了下 PLT_CtrlPointReference 的实现,原来它实现了一种机制,类似于java语言的对象引用,如果一个对象没有引用了,就会自动释放。


PLT_CtrlPointReference 因为上在 C++ 语言上实现了,巧妙的使用了 析构 函数的应用


先上代码,我把 PLT_CtrlPointReference 的实现简单整理了下,应该可以在各种平台编译测试

typedef unsigned int  NPT_Cardinal;template <typename T>class NPT_Reference{public:    // constructors and destructor    NPT_Reference() : m_Object(NULL), m_Counter(NULL) {}    explicit NPT_Reference(T* object, bool thread_safe = true) :         m_Object(object),         m_Counter(object?new NPT_Cardinal(1):NULL){}NPT_Reference(const NPT_Reference<T>& ref) :        m_Object(ref.m_Object), m_Counter(ref.m_Counter){        if (m_Counter) ++(*m_Counter);    }// this methods should be private, but this causes a problem on some    // compilers, because we need this function in order to implement    // the cast operator operator NPT_Reference<U>() below, which would    // have to be marked as a friend, and friend declarations with the     // same class name confuses some compilers    NPT_Reference(T* object, NPT_Cardinal* counter) :         m_Object(object), m_Counter(counter) {         if (m_Counter) ++(*m_Counter);    }    ~NPT_Reference() {        Release();    }// overloaded operators    NPT_Reference<T>& operator=(const NPT_Reference<T>& ref) {        if (this != &ref) {            Release();            m_Object = ref.m_Object;            m_Counter = ref.m_Counter;                                    if (m_Counter) ++(*m_Counter);                    }        return *this;    }NPT_Reference<T>& operator=(T* object) {        Release();        m_Object  = object;        m_Counter = object?new NPT_Cardinal(1):NULL;                return *this;    }T& operator*() const { return *m_Object; }    T* operator->() const { return m_Object; }    bool operator==(const NPT_Reference<T>& ref) const {        return m_Object == ref.m_Object;    }     bool operator!=(const NPT_Reference<T>& ref) const {        return m_Object != ref.m_Object;    }// overloaded cast operators    template <typename U> operator NPT_Reference<U>() {        return NPT_Reference<U>(m_Object, m_Counter);    }    // methods    /**     * Returns the naked pointer value.     */    T* AsPointer() const { return m_Object; }        /**     * Returns the reference counter value.     */    NPT_Cardinal GetCounter() const { return *m_Counter; }        /**     * Returns whether this references a NULL object.     */    bool IsNull()  const { return m_Object == NULL; }        /**     * Detach the reference from the shared object.     * The reference count is decremented, but the object is not deleted if the     * reference count becomes 0.     * After the method returns, this reference does not point to any shared object.     */    void Detach() {        Release(true);            }private:    // methods    void Release(bool detach_only = false) {        bool last_reference = false;                            if (m_Counter && --(*m_Counter) == 0) {            delete m_Counter;            if (!detach_only) delete m_Object;            last_reference = true;        }                m_Counter = NULL;        m_Object  = NULL;     }// members    T*            m_Object;    NPT_Cardinal* m_Counter;};class PLT_CtrlPoint{public:PLT_CtrlPoint(){printf("create\n");}; ~PLT_CtrlPoint(){printf("destroy\n");};};

接下来,我们做测试


typedef NPT_Reference<PLT_CtrlPoint> PLT_CtrlPointReference;static void test(void){PLT_CtrlPointReference g_dlna_ctrl_point;g_dlna_ctrl_point = (PLT_CtrlPointReference)new PLT_CtrlPoint();}int _tmain(int argc, _TCHAR* argv[]){test();return 0;}

我们看到,g_dlna_ctrl_point 进行了 new 操作但是没有进行 delete 操作

但是测试的结果是:

destroy 打印出来了


这是因为 g_dlna_ctrl_point 是局部对象,走过了 test() 函数的生存期后,会自动调用 g_dlna_ctrl_point 对象的析构函数,然后引用计数器减1,发现引用计数器为 0 的话,就真正delete 对象


我们再来一个测试

PLT_CtrlPointReference g_dlna_ctrl_point;static void test(void){g_dlna_ctrl_point = (PLT_CtrlPointReference)new PLT_CtrlPoint();}int _tmain(int argc, _TCHAR* argv[]){test();return 0;}

g_dlna_ctrl_point 现在是全局的了,可见 调用完 test() 函数后,没有进行释放操作

这个时候,我加上 g_dlna_ctrl_point = NULL; 操作,见下面代码

PLT_CtrlPointReference g_dlna_ctrl_point;static void test(void){g_dlna_ctrl_point = (PLT_CtrlPointReference)new PLT_CtrlPoint();}int _tmain(int argc, _TCHAR* argv[]){test();g_dlna_ctrl_point = NULL;return 0;}

这个时候可看到:

destroy 被打印出来了


这样,局部的对象,不需要的时候,不需要理会,它自己会消失

全局的对象,不需要的时候,赋值为 NULL 或者是其它的值,类似java的机制,那么这个对象就消失了

了解了这些,再分析这些代码,就清楚了

原创粉丝点击