boost::shared_ptr shared_from_this
2014-07-28 12:44
218 查看
需要将指针再构造为一个shared_ptr时, 可以让类继承自enable_shared_from_this; 然后类内部使用shared_from_this()生成一个shared_ptr。
不能直接将this构造为一个shared_ptr,因为有可能其他的shared_ptr已经包含了this; 这种情况下, 相当于使用raw pointer构造了多个shared_ptr、 会多次释放。
P *p = new P;
shared_ptr p1(p);
shared_ptr p2(p);
////error。。。。会导致p被多次delete。
转
http://www.cnblogs.com/lzjsky/archive/2011/05/05/2037363.html http://blog.csdn.net/salutlu/article/details/12515877 http://www.cnblogs.com/livingintruth/archive/2012/03/05/2367253.html http://hi.baidu.com/cpuramdisk/item/7c2f8d77385e0f29d7a89cf0
我在写asio过程中, 基本上都碰到过了。
1) 同时也解释了在构造函数中不能调用shared_from_this()的错误,我写asio程序时想在构造中发起异步connect,结果运行出错。。。 无意中招。
2) 定义在栈上的对象、或者没有使用shared_ptr管理的raw pointer, 无需继承自enable_shared_from_this、不能调用shared_from_this。
asio::async_read(socket,...)
socket.async_read_some
两者区别:
asio::async_read填充满buffer或者符合指定条件才返回
socket.async_read_some读到数据就返回。
from http://blog.csdn.net/hbhhww/article/details/7469923
不能直接将this构造为一个shared_ptr,因为有可能其他的shared_ptr已经包含了this; 这种情况下, 相当于使用raw pointer构造了多个shared_ptr、 会多次释放。
P *p = new P;
shared_ptr p1(p);
shared_ptr p2(p);
////error。。。。会导致p被多次delete。
转
http://www.cnblogs.com/lzjsky/archive/2011/05/05/2037363.html http://blog.csdn.net/salutlu/article/details/12515877 http://www.cnblogs.com/livingintruth/archive/2012/03/05/2367253.html http://hi.baidu.com/cpuramdisk/item/7c2f8d77385e0f29d7a89cf0
我在写asio过程中, 基本上都碰到过了。
1) 同时也解释了在构造函数中不能调用shared_from_this()的错误,我写asio程序时想在构造中发起异步connect,结果运行出错。。。 无意中招。
2) 定义在栈上的对象、或者没有使用shared_ptr管理的raw pointer, 无需继承自enable_shared_from_this、不能调用shared_from_this。
asio::async_read(socket,...)
socket.async_read_some
两者区别:
asio::async_read填充满buffer或者符合指定条件才返回
socket.async_read_some读到数据就返回。
from http://blog.csdn.net/hbhhww/article/details/7469923
相关文章推荐
- boost::weak_ptr和enable_shared_from_this
- boost::shared_ptr递归调用解决办法--boost::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::shared_ptr和shared_from_this()时,程序崩溃
- boost::weak_ptr和enable_shared_from_this
- How to get a boost::shared_ptr from this
- boost::weak_ptr和enable_shared_from_this
- Boost 库 Enable_shared_from_this 实现原理分析
- 如何把this指针转换成boost的shared_ptr
- boost::enable_shared_from_this的部分实现研究
- boost shared_ptr: enable_shared_from_this验证
- 【Boost】boost::shared_from_this值得注意的地方
- Boost 库 enable_shared_from_this 实现原理分析
- weak_ptr,enable_shared_from_this 的使用举例
- 对enable_shared_from_this、weak_ptr、shared_ptr的理解
- 【Boost】boost::shared_from_this值得注意的地方
- boost shared_ptr: enable_shared_from_this验证
- boost::shared_from_this值得注意的地方