flushthink

just for essential skill
随笔 - 14, 文章 - 0, 评论 - 30, 引用 - 0
数据加载中……

enable_shared_from_this、weak_ptr、shared_ptr

网上说weak_ptr是shared_ptr的观察员,weak_ptr不会干扰shared_ptr机制,当weak_ptr所观察的shared_ptr要释放对象时,weak_ptr的指针将被置空,避免空悬指针。
weak_ptr只能通过shared_ptr或weak_ptr构造。

对于一个shared_ptr,它分别对强引用和弱引用都做了计数。

上图是下面代码的调试信息。
#include "stdafx.h"
#include 
<iostream>
#include 
<boost/shared_ptr.hpp>
#include 
<boost/weak_ptr.hpp>
#include 
<boost/enable_shared_from_this.hpp>

struct A
{};

int _tmain(int argc, _TCHAR* argv[])
{
    boost::shared_ptr
<A> a(new A);

    
return 0;
}

当一个shared_ptr构造,use_count_和weak_count_都被赋值为1。



上图是下面代码调试信息。
#include "stdafx.h"
#include 
<iostream>
#include 
<boost/shared_ptr.hpp>
#include 
<boost/weak_ptr.hpp>
#include 
<boost/enable_shared_from_this.hpp>

struct A
{};

class B 
{
public:
    B()
        : mA(
new A)
    
{
    }


    
/// 把指针返回出去
    boost::shared_ptr<A> get()
    
{
        
return mA;
    }

private:
    boost::shared_ptr
<A> mA;
}
;

int _tmain(int argc, _TCHAR* argv[])
{
    
{
        B b;
        boost::weak_ptr
<A> wp1 = b.get();
        boost::weak_ptr
<A> wp2 = b.get();
        boost::shared_ptr
<A> sp1 = b.get();
        boost::shared_ptr
<A> sp2 = b.get();
    }


    
return 0;
}



当一个对象返回出一个自身对象shared_ptr,一般用enable_shared_from_this,而enable_shared_from_this就是用weak_ptr来实现的。

 

#include "stdafx.h"
#include 
<iostream>
#include 
<boost/shared_ptr.hpp>
#include 
<boost/weak_ptr.hpp>
#include 
<boost/enable_shared_from_this.hpp>

struct A : public boost::enable_shared_from_this<A>
{

    boost::shared_ptr
<A> get()
    
{
        boost::shared_ptr
<A> holder(new A);
        
return holder;
    }

}
;

int _tmain(int argc, _TCHAR* argv[])
{
    
    A a;
    boost::shared_ptr
<A> aa = a.get();
    
return 0;
}

上述代码的对象构造是:enable_shared_from_this<A>,然后是A,再就是shared_ptr,所以必须在构造完成之后再进行对象智能指针的创建。这是一种循环依赖关系,一般循环依赖都要用到weak_ptr。

针对循环依赖的shared_ptr如下:
#include "stdafx.h"
#include 
<iostream>
#include 
<boost/shared_ptr.hpp>
#include 
<boost/weak_ptr.hpp>
#include 
<boost/enable_shared_from_this.hpp>

/// 相互依赖
struct Product;
struct ProductManager;

typedef boost::shared_ptr
<Product> ProductPtr;
typedef boost::shared_ptr
<ProductManager> ProductManagerPtr;

struct Product
{
    Product()
    
{
        std::cout 
<< "Product 构造" << std::endl;
    }

    
~Product()
    
{
        std::cout 
<< "Product 析构" << std::endl;
    }

    ProductManagerPtr mMgr;
}
;

struct ProductManager
{
    ProductManager()
    
{
        std::cout 
<< "ProductManager 构造" << std::endl;
    }

    
~ProductManager()
    
{
        std::cout 
<< "ProductManager 析构" << std::endl;
    }

    ProductPtr mProduct;
}
;

int _tmain(int argc, _TCHAR* argv[])
{
    ProductPtr product(
new Product);
    ProductManagerPtr productMgr(
new ProductManager);
    product
->mMgr = productMgr;
    productMgr
->mProduct = product;
    
return 0;
}

结果是:


不能释放对象。
对于这种情况要这么做:

struct Product
{
    Product()
    
{
        std::cout 
<< "Product 构造" << std::endl;
    }

    
~Product()
    
{
        std::cout 
<< "Product 析构" << std::endl;
    }

    boost::weak_ptr
<ProductManager> mMgr;
}
;


 

posted on 2009-09-17 15:14 tiny 阅读(2096) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理