boost shared_ptr: enable_shared_from_this验证
来源:互联网 发布:宝鸡广电网络客服电话 编辑:程序博客网 时间:2024/05/15 19:21
项目中使用boost::asio,其中的异步调用往往会有一个handler参数,这个参数一般使用动态内存申请的对象和其成员函数构建。
因此如果在asio线程和对象管理线程中间没有适当的同步机制,则很容易造成内存访问错误,如pure virtual method called等。这时候可以使用boost::shared_ptr和enable_shared_from_this(从类实例内获取管理自身的shared_ptr拷贝,引用计数会加1,这个拷贝如果不失效则类实例不能析构)来解决动态内存管理问题:
下面是一个验证enable_shared_from_this的例子
#include <boost/enable_shared_from_this.hpp>#include <boost/shared_ptr.hpp>#include <boost/bind.hpp>#include <boost/function.hpp>class Good;class Good: public boost::enable_shared_from_this<Good>{private: virtual int callback() = 0;public: void init_cb() { cb = boost::bind(&Good::callback,shared_from_this()); } void run() { cb(); } boost::shared_ptr<Good> get_shared_ptr() { return shared_from_this(); } virtual ~Good() { std::cout<<"~Good"<<std::endl; }private: boost::function<int()> cb;};class SonOfGood:public Good{ virtual int callback(){ std::cout<<"Callback"<<std::endl; }public: virtual ~SonOfGood() { std::cout<<"~SonOfGood"<<std::endl; }};void func(boost::shared_ptr<Good> & gp1){ std::cout << "gp1.use_count() = " << gp1.use_count() << '\n'; boost::shared_ptr<Good> cb = gp1->get_shared_ptr(); std::cout << "gp1.use_count() = " << gp1.use_count() << '\n'; // 计数应该加1}int main(){ boost::shared_ptr<Good> gp1(new SonOfGood); std::cout << "gp1.use_count() = " << gp1.use_count() << '\n'; // 应该为1 func(gp1); std::cout << "gp1.use_count() = " << gp1.use_count() << '\n'; // 应该为1 std::cout << "gp1.use_count() = " << gp1.use_count() << '\n'; // 应该为1 gp1->init_cb(); // 从gp1创建了boost::function<int()>,使用了shared_from_this(),use_count会加1 std::cout << "gp1.use_count() = " << gp1.use_count() << '\n'; // 应该为2 gp1->run(); std::cout << "gp1.use_count() = " << gp1.use_count() << '\n'; // 应该为2}
具体使用还可以参考mongodb代码,其中的message_server_asio.cpp文件中有非常好的例子。
- boost shared_ptr: enable_shared_from_this验证
- boost::shared_ptr递归调用解决办法--boost::enable_shared_from_this
- BOOST enable_shared_from_this
- boost::enable_shared_from_this
- enable_shared_from_this、weak_ptr、shared_ptr
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost::enable_shared_from_this分析
- boost::enable_shared_from_this分析
- boost::enable_shared_from_this的用法
- boost::weak_ptr和enable_shared_from_this
- boost>shared_ptr
- Boost - shared_ptr
- boost>shared_ptr
- 【转】常用的版本控制软件
- 音视频即时通讯中流媒体的简介
- POJ 3094
- linux内核SPI总线驱动分析(一)
- Struts2自定义拦截器
- boost shared_ptr: enable_shared_from_this验证
- JAVA向文件中追加内容
- Unity3D 协同程序和让步
- opencv_traincascade支持Haar和LBP特征
- STL-1
- IPAD模拟器HOME键消失问题
- Unity脚本周期关系
- 数据库中的记录如何递归添加TreeView
- kernel IPv6 路由学习笔记(1)原理.