Difference between revisions of "cpp/memory/enable shared from this"
(added Wrong Usage Example - important since many programmers fail with that) |
(slipped in "publicly" for the inheritance from shared_from_this: http://stackoverflow.com/questions/38165889) |
||
Line 7: | Line 7: | ||
{{tt|std::enable_shared_from_this}} allows an object {{tt|t}} that is currently managed by a {{lc|std::shared_ptr}} named {{tt|pt}} to safely generate additional {{lc|std::shared_ptr}} instances {{tt|pt1, pt2, ...}} that all share ownership of {{tt|t}} with {{tt|pt}}. | {{tt|std::enable_shared_from_this}} allows an object {{tt|t}} that is currently managed by a {{lc|std::shared_ptr}} named {{tt|pt}} to safely generate additional {{lc|std::shared_ptr}} instances {{tt|pt1, pt2, ...}} that all share ownership of {{tt|t}} with {{tt|pt}}. | ||
− | + | Publicly inheriting from {{tt|std::enable_shared_from_this<T>}} provides the type {{tt|T}} with a member function {{tt|shared_from_this}}. If an object {{tt|t}} of type {{tt|T}} is managed by a {{c|std::shared_ptr<T>}} named {{tt|pt}}, then calling {{tt|T::shared_from_this}} will return a new {{c|std::shared_ptr<T>}} that shares ownership of {{tt|t}} with {{tt|pt}}. | |
Note that prior to calling {{tt|shared_from_this}} on an object {{tt|t}}, there must be a {{lc|std::shared_ptr}} that owns {{tt|t}}. | Note that prior to calling {{tt|shared_from_this}} on an object {{tt|t}}, there must be a {{lc|std::shared_ptr}} that owns {{tt|t}}. |
Revision as of 11:32, 13 July 2016
Defined in header <memory>
|
||
template< class T > class enable_shared_from_this; |
(since C++11) | |
std::enable_shared_from_this
allows an object t
that is currently managed by a std::shared_ptr named pt
to safely generate additional std::shared_ptr instances pt1, pt2, ...
that all share ownership of t
with pt
.
Publicly inheriting from std::enable_shared_from_this<T>
provides the type T
with a member function shared_from_this
. If an object t
of type T
is managed by a std::shared_ptr<T> named pt
, then calling T::shared_from_this
will return a new std::shared_ptr<T> that shares ownership of t
with pt
.
Note that prior to calling shared_from_this
on an object t
, there must be a std::shared_ptr that owns t
.
Also note that enable_shared_from_this
provides an alternative to an expression like std::shared_ptr<T>(this), which is likely to result in this being destructed more than once by multiple owners that are unaware of each other.
Contents |
Member functions
constructs an enabled_shared_from_this object (protected member function) | |
destroys an enable_shared_from_this object (protected member function) | |
returns a reference to this (protected member function) | |
returns a shared_ptr which shares ownership of *this (public member function) | |
(C++17) |
returns the weak_ptr which shares ownership of *this (public member function) |
Member objects
Member name | Definition |
weak_this (private)(C++17)
|
std::weak_ptr object tracking the control block of the first shared owner of *this |
Notes
A common(until C++17)standard(since C++17) implementation for enable_shared_from_this
is to hold a weak reference (such as std::weak_ptr) to this. The constructors of std::shared_ptr detect the presence of an enable_shared_from_this
base and assign the newly created std::shared_ptr to the internally stored weak reference. Constructing a std::shared_ptr for an object that is already managed by another std::shared_ptr will not consult the internally stored weak reference and thus will lead to undefined behavior.
Example
#include <memory> #include <iostream> struct Good: std::enable_shared_from_this<Good> { std::shared_ptr<Good> getptr() { return shared_from_this(); } }; struct Bad { std::shared_ptr<Bad> getptr() { return std::shared_ptr<Bad>(this); } ~Bad() { std::cout << "Bad::~Bad() called\n"; } }; int main() { // Good: the two shared_ptr's share the same object std::shared_ptr<Good> gp1(new Good); std::shared_ptr<Good> gp2 = gp1->getptr(); std::cout << "gp2.use_count() = " << gp2.use_count() << '\n'; // Bad, each shared_ptr thinks it's the only owner of the object std::shared_ptr<Bad> bp1(new Bad); std::shared_ptr<Bad> bp2 = bp1->getptr(); std::cout << "bp2.use_count() = " << bp2.use_count() << '\n'; } // UB: double-delete of Bad
Output:
gp2.use_count() = 2 bp2.use_count() = 1 Bad::~Bad() called Bad::~Bad() called *** glibc detected *** ./test: double free or corruption
Additional Notes
It is permitted to call shared_from_this
only on a previously shared object, i.e. on an object managed by std::shared_ptr<T>. Otherwise the behavior is undefined(until C++17)std::bad_weak_ptr is thrown (by the shared_ptr constructor from a default-constructed weak_this
)(since C++17).
Wrong Usage Example
#include <memory> #include <iostream> struct Good: std::enable_shared_from_this<Good> { std::shared_ptr<Good> getptr() { return shared_from_this(); } }; int main() { try { Good not_so_good; // shared_from_this is called without having std::shared_ptr owning the caller std::shared_ptr<Good> gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) std::cout << e.what() << '\n'; } // Same problem as above, no owning std::shared_ptr for the caller try { std::unique_ptr<Good> not_so_good_p = std::unique_ptr<Good>(new Good); std::shared_ptr<Good> gp2 = not_so_good_p->getptr(); } catch(std::bad_weak_ptr& e) { // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) std::cout << e.what() << '\n'; } }
Output:
bad_weak_ptr bad_weak_ptr
See also
(C++11) |
smart pointer with shared object ownership semantics (class template) |