shared_ptr enable_shared_from_this - C++11, 3 of n
2012-09-03 00:14
393 查看
#include <memory>
struct S: enable_shared_from_this<S>
{
};
Don't do this:
S *p = new S;
shared_ptr<S> sp2 = p->shared_from_this(); // throws std::bad_weak_ref
Do this:
shared_ptr<S> p(new S);
shared_ptr<S> sp2 = p->shared_from_this();
Rule: when a class inherits from enable_shared_from_this, it almost always means it should work with shared_ptr together.
What happened behind the scene for shared_ptr<S> p(new S) ?
(1) enable_shared_from_this construct is called
(2) S constructor is called
(3) shared_ptr constructor is called (the magic happens here)
|
| -> __enable_shared_from_this_helper is called in shared_ptr constructor
|
|-> enable_shared_from_this::_M_weak_assign is called
|
|-> weak_ptr<_Tp>::._M_assign(__p, __n);
(called on mutable enable_shared_from_this::_M_weak_this instance in
struct S object. This calls sets (inits) the weak_ptr in struct S object)
|
|-> Set weak_ptr<_Tp>::_M_ptr and
weak_ptr<_Tp>:: _M_refcount (__weak_count<_Lp>)
![](http://img.my.csdn.net/uploads/201302/02/1359819302_6433.png)
Why the first case throws ?
// enabled_shared_from_this::shared_from_this
__shared_ptr<_Tp, _Lp> shared_from_this() { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
// __shared_ptr constructor with weak_ptr
template<typename _Tp1>
explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r) : _M_refcount(__r._M_refcount)// may throw
{
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
// It is now safe to copy __r._M_ptr, as
// _M_refcount(__r._M_refcount) did not throw.
_M_ptr = __r._M_ptr;
}
// __share_count constructor with __weak_count
// Throw bad_weak_ptr when __r._M_get_use_count() == 0.
explicit __shared_count(const __weak_count<_Lp>& __r): _M_pi(__r._M_pi)
{
if (_M_pi != 0)
_M_pi->_M_add_ref_lock();
else
__throw_bad_weak_ptr();
}
struct S: enable_shared_from_this<S>
{
};
Don't do this:
S *p = new S;
shared_ptr<S> sp2 = p->shared_from_this(); // throws std::bad_weak_ref
Do this:
shared_ptr<S> p(new S);
shared_ptr<S> sp2 = p->shared_from_this();
Rule: when a class inherits from enable_shared_from_this, it almost always means it should work with shared_ptr together.
What happened behind the scene for shared_ptr<S> p(new S) ?
(1) enable_shared_from_this construct is called
(2) S constructor is called
(3) shared_ptr constructor is called (the magic happens here)
|
| -> __enable_shared_from_this_helper is called in shared_ptr constructor
|
|-> enable_shared_from_this::_M_weak_assign is called
|
|-> weak_ptr<_Tp>::._M_assign(__p, __n);
(called on mutable enable_shared_from_this::_M_weak_this instance in
struct S object. This calls sets (inits) the weak_ptr in struct S object)
|
|-> Set weak_ptr<_Tp>::_M_ptr and
weak_ptr<_Tp>:: _M_refcount (__weak_count<_Lp>)
![](http://img.my.csdn.net/uploads/201302/02/1359819302_6433.png)
Why the first case throws ?
// enabled_shared_from_this::shared_from_this
__shared_ptr<_Tp, _Lp> shared_from_this() { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
// __shared_ptr constructor with weak_ptr
template<typename _Tp1>
explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r) : _M_refcount(__r._M_refcount)// may throw
{
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
// It is now safe to copy __r._M_ptr, as
// _M_refcount(__r._M_refcount) did not throw.
_M_ptr = __r._M_ptr;
}
// __share_count constructor with __weak_count
// Throw bad_weak_ptr when __r._M_get_use_count() == 0.
explicit __shared_count(const __weak_count<_Lp>& __r): _M_pi(__r._M_pi)
{
if (_M_pi != 0)
_M_pi->_M_add_ref_lock();
else
__throw_bad_weak_ptr();
}
相关文章推荐
- 对enable_shared_from_this、weak_ptr、shared_ptr的理解
- C++学习 std::tr1::shared_ptr、std::tr1::weak_ptr及std::tr1::enable_shared_from_this
- enable_shared_from_this,关于在类的内部获取该对象shared_ptr指针的问题
- 用enable_shared_from_this在类的成员函数中获得指向当向前对象的shared_ptr
- boost::weak_ptr和enable_shared_from_this
- boost::shared_ptr递归调用解决办法--boost::enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- 如何用enable_shared_from_this 来得到指向自身的shared_ptr 及对enable_shared_from_this 的理解
- shared_ptr,shared_ptr,enable_shared_from_this的理解
- boost::weak_ptr和enable_shared_from_this
- weak_ptr 和enable_shared_from_this
- share_ptr 与enable_shared_from_this
- weak_ptr,enable_shared_from_this 的使用举例
- std::tr1::shared_ptr、std::tr1::weak_ptr及std::tr1::enable_shared_from_this
- 关于std::shared_ptr与std::enable_shared_from_this循环引用导致的问题
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost shared_ptr: enable_shared_from_this验证
- C++11新特性之十一:enable_shared_from_this
- 从 this 指针获得一个shared_ptr: enable_shared_from_this