歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++智能指針 weak_ptr

C++智能指針 weak_ptr

日期:2017/3/1 9:08:35   编辑:Linux編程

  weak_ptr 是一種不控制對象生命周期的智能指針, 它指向一個 shared_ptr 管理的對象. 進行該對象的內存管理的是那個強引用的 shared_ptr. weak_ptr只是提供了對管理對象的一個訪問手段.
  weak_ptr 設計的目的是為配合 shared_ptr 而引入的一種智能指針來協助 shared_ptr 工作, 它只可以從一個 shared_ptr 或另一個 weak_ptr 對象構造, 它的構造和析構不會引起引用記數的增加或減少.
  定義在 memory 文件中(非memory.h), 命名空間為 std.

  weak_ptr 使用:

std::shared_ptr<int> sp(new int(10));
std::weak_ptr<int> wp(sp);
wp = sp;
printf("%d\n", wp.use_count()); // 1
wp.reset();
printf("%d\n", wp); // 0

// 檢查 weak_ptr 內部對象的合法性.
if (std::shared_ptr<int> sp = wp.lock())
{
}

成員函數

weak_ptr 沒有重載*和->但可以使用 lock 獲得一個可用的 shared_ptr 對象. 注意, weak_ptr 在使用前需要檢查合法性.

expired 用於檢測所管理的對象是否已經釋放, 如果已經釋放, 返回 true; 否則返回 false.
lock 用於獲取所管理的對象的強引用(shared_ptr). 如果 expired 為 true, 返回一個空的 shared_ptr; 否則返回一個 shared_ptr, 其內部對象指向與 weak_ptr 相同.
use_count 返回與 shared_ptr 共享的對象的引用計數.
reset 將 weak_ptr 置空.
weak_ptr 支持拷貝或賦值, 但不會影響對應的 shared_ptr 內部對象的計數.

使用 weak_ptr 解決 shared_ptr 因循環引有不能釋放資源的問題

使用 shared_ptr 時, shared_ptr 為強引用, 如果存在循環引用, 將導致內存洩露. 而 weak_ptr 為弱引用, 可以避免此問題, 其原理:
  對於弱引用來說, 當引用的對象活著的時候弱引用不一定存在. 僅僅是當它存在的時候的一個引用, 弱引用並不修改該對象的引用計數, 這意味這弱引用它並不對對象的內存進行管理.
  weak_ptr 在功能上類似於普通指針, 然而一個比較大的區別是, 弱引用能檢測到所管理的對象是否已經被釋放, 從而避免訪問非法內存。
注意: 雖然通過弱引用指針可以有效的解除循環引用, 但這種方式必須在程序員能預見會出現循環引用的情況下才能使用, 也可以是說這個僅僅是一種編譯期的解決方案, 如果程序在運行過程中出現了循環引用, 還是會造成內存洩漏.

         class CB;
        class CA;
     
        class CA
        {
        public:
            CA(){}
            ~CA(){PRINT_FUN();}
     
            void Register(const std::shared_ptr<CB>& sp)
            {
                m_spb = sp;
            }
     
        private:
            std::weak_ptr<CB> m_spb;
        };
     
        class CB
        {
        public:
            CB(){};
            ~CB(){PRINT_FUN();};
     
            void Register(const std::shared_ptr<CA>& sp)
            {
                m_spa = sp;
            }
     
        private:
            std::shared_ptr<CA> m_spa;
        };
     
        std::shared_ptr<CA> spa(new CA);
        std::shared_ptr<CB> spb(new CB);
     
        spb->Register(spa);
        spa->Register(spb);
        printf("%d\n", spb.use_count()); // 1
        printf("%d\n", spa.use_count()); // 2

VC中的源碼實現

template<class _Ty>
class weak_ptr
    : public _Ptr_base<_Ty>
{    // class for pointer to reference counted resource
    typedef typename _Ptr_base<_Ty>::_Elem _Elem;

public:
    weak_ptr()
    {    // construct empty weak_ptr object
    }

    template<class _Ty2>
    weak_ptr(const shared_ptr<_Ty2>& _Other,
        typename enable_if<is_convertible<_Ty2 *, _Ty *>::value,
        void *>::type * = 0)
    {    // construct weak_ptr object for resource owned by _Other
        this->_Resetw(_Other);
    }

    weak_ptr(const weak_ptr& _Other)
    {    // construct weak_ptr object for resource pointed to by _Other
        this->_Resetw(_Other);
    }

    template<class _Ty2>
    weak_ptr(const weak_ptr<_Ty2>& _Other,
        typename enable_if<is_convertible<_Ty2 *, _Ty *>::value,
        void *>::type * = 0)
    {    // construct weak_ptr object for resource pointed to by _Other
        this->_Resetw(_Other);
    }

    ~weak_ptr()
    {    // release resource
        this->_Decwref();
    }

    weak_ptr& operator=(const weak_ptr& _Right)
    {    // assign from _Right
        this->_Resetw(_Right);
        return (*this);
    }

    template<class _Ty2>
    weak_ptr& operator=(const weak_ptr<_Ty2>& _Right)
    {    // assign from _Right
        this->_Resetw(_Right);
        return (*this);
    }

    template<class _Ty2>
    weak_ptr& operator=(shared_ptr<_Ty2>& _Right)
    {    // assign from _Right
        this->_Resetw(_Right);
        return (*this);
    }

    void reset()
    {    // release resource, convert to null weak_ptr object
        this->_Resetw();
    }

    void swap(weak_ptr& _Other)
    {    // swap pointers
        this->_Swap(_Other);
    }

    bool expired() const
    {    // return true if resource no longer exists
        return (this->_Expired());
    }

    shared_ptr<_Ty> lock() const
    {    // convert to shared_ptr
        return (shared_ptr<_Elem>(*this, false));
    }
};

Copyright © Linux教程網 All Rights Reserved