weak_ptr解決shared_ptr環狀引用所引發的內存泄漏

循環引用:ios

引用計數是一種便利的內存管理機制,但它有一個很大的缺點,那就是不能管理循環引用的對象。一個簡單的例子以下:c++

#include<string>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
class parent;
class children;
typedef boost::shared_ptr<parent> parent_ptr;
typedef boost::shared_ptr<children> children_ptr;

class parent
{
public:
    ~parent() { std::cout <<"destroying parent\n"; }
public:
    children_ptr children;
};
class children
{
public:
    ~children() { std::cout <<"destroying children\n"; }
public:
    parent_ptr parent;
};
void test()
{
    parent_ptr father(new parent());
    children_ptr son(new children);

    father->children = son;
    son->parent = father;
}

void main()
{
    std::cout<<"begin test...\n";
    test();
    std::cout<<"end test.\n";
}程序員

運行該程序能夠看到,即便退出了test函數後,因爲parent和children對象互相引用,它們的引用計數都是1,不能自動釋放,而且此時這兩個對象再沒法訪問到。這就引發了c++中那臭名昭著的內存泄漏。app

通常來說,解除這種循環引用有下面有三種可行的方法:函數

  1. 當只剩下最後一個引用的時候須要手動打破循環引用釋放對象。spa

  2. 當parent的生存期超過children的生存期的時候,children改成使用一個普通指針指向parent。.net

  3. 使用弱引用的智能指針打破這種循環引用。指針

雖然這三種方法均可行,但方法1和方法2都須要程序員手動控制,麻煩且容易出錯。這裏主要介紹一下第三種方法和boost中的弱引用的智能指針boost::weak_ptr。對象

強引用和弱引用blog

一個強引用當被引用的對象活着的話,這個引用也存在(就是說,當至少有一個強引用,那麼這個對象就不能被釋放)。boost::share_ptr就是強引用。

相對而言,弱引用當引用的對象活着的時候不必定存在。僅僅是當它存在的時候的一個引用。弱引用並不修改該對象的引用計數,這意味這弱引用它並不對對象的內存進行管理,在功能上相似於普通指針,然而一個比較大的區別是,弱引用能檢測到所管理的對象是否已經被釋放,從而避免訪問非法內存。

boost::weak_ptr

boost::weak_ptr<T>是boost提供的一個弱引用的智能指針,它的聲明能夠簡化以下:

namespace boost {

    template<typename T>class weak_ptr {
    public:
        template <typename Y>
        weak_ptr(const shared_ptr<Y>& r);

        weak_ptr(const weak_ptr& r);

        ~weak_ptr();

        T* get() const; 
        bool expired() const;
        shared_ptr<T> lock() const;
    }; 
}

能夠看到,boost::weak_ptr必須從一個boost::share_ptr或另外一個boost::weak_ptr轉換而來,這也說明,進行該對象的內存管理的是那個強引用的boost::share_ptr。boost::weak_ptr只是提供了對管理對象的一個訪問手段。

boost::weak_ptr除了對所管理對象的基本訪問功能(經過get()函數)外,還有兩個經常使用的功能函數:expired()用於檢測所管理的對象是否已經釋放;lock()用於獲取所管理的對象的強引用指針。

經過boost::weak_ptr來打破循環引用

因爲弱引用不更改引用計數,相似普通指針,只要把循環引用的一方使用弱引用,便可解除循環引用。對於上面的那個例子來講,只要把children的定義改成以下方式,便可解除循環引用:

class children
{
public:
    ~children() { std::cout <<"destroying children\n"; }

public:
    boost::weak_ptr<parent> parent;
};

最後值得一提的是,雖然經過弱引用指針能夠有效的解除循環引用,但這種方式必須在程序員能預見會出現循環引用的狀況下才能使用,也能夠是說這個僅僅是一種編譯期的解決方案,若是程序在運行過程當中出現了循環引用,仍是會形成內存泄漏的。所以,不要認爲只要使用了智能指針便能杜絕內存泄漏。畢竟,對於C++來講,因爲沒有垃圾回收機制,內存泄漏對每個程序員來講都是一個很是頭痛的問題。

 

vs2008示例代碼:

[cpp] view plain copy

  1. #include "stdafx.h"  
  2. #include <string>  
  3. #include <iostream>  
  4.   
  5. #include "memory"  
  6. #define _CRTDBG_MAP_ALLOC  
  7.   
  8. #include <stdlib.h>  
  9.   
  10. #include <crtdbg.h>  
  11.   
  12. #define new new( _CLIENT_BLOCK, __FILE__, __LINE__)  
  13.   
  14. #define _LOOP_POINTER_  
  15.   
  16.   
  17. class parent;  
  18. class children;  
  19.   
  20. #ifdef _LOOP_POINTER_  
  21. typedef std::tr1::shared_ptr<parent> parent_ptr;  
  22. #else  
  23. typedef std::tr1::weak_ptr<parent> parent_ptr;  
  24. #endif  
  25.   
  26. typedef std::tr1::shared_ptr<children> children_ptr;  
  27.   
  28. class parent  
  29. {  
  30. public:  
  31.     ~parent() { std::cout <<"destroying parent\n"; }  
  32.   
  33. public:  
  34.     children_ptr children;  
  35. };  
  36.   
  37. class children  
  38. {  
  39. public:  
  40.     ~children() { std::cout <<"destroying children\n"; }  
  41.   
  42. public:  
  43.     parent_ptr parent;  
  44. };  
  45.   
  46.   
  47. void test()  
  48. {  
  49.     children_ptr son(new children);  
  50. #ifdef _LOOP_POINTER_  
  51.     parent_ptr father(new parent());  
  52. #else  
  53.     std::tr1::shared_ptr<parent> father(new parent());      
  54.     son->parent = parent_ptr(father);  
  55. #endif  
  56.   
  57.     father->children = son;  
  58.       
  59. }  
  60.   
  61.   
  62.   
  63.   
  64. int _tmain(int argc, _TCHAR* argv[])  
  65. {  
  66.     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);  
  67.       
  68.     std::cout<<"begin test...\n";  
  69.     test();  
  70.     std::cout<<"end test.\n";  
  71.   
  72.     return 0;  
  73. }  
相關文章
相關標籤/搜索