Trivial cases of shared_ptr and weak_ptr failing
You must inherit publicly from std::enable_shared_from_this. Inheriting privately doesn’t help – std::shared_ptr can’t access the base class and set it up properly.
You must inherit publicly from std::enable_shared_from_this. Inheriting privately doesn’t help – std::shared_ptr can’t access the base class and set it up properly.
From the Boost 1.42 source code (<boost/shared_ptr/weak_ptr.hpp> line 155): shared_ptr<T> lock() const // never throws { return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() ); } ergo, James McNellis’s comment is correct; it’s the cost of copy-constructing a shared_ptr.
You can use two calls to owner_before to check equality with a default constructed (empty) weak pointer: template <typename T> bool is_uninitialized(std::weak_ptr<T> const& weak) { using wt = std::weak_ptr<T>; return !weak.owner_before(wt{}) && !wt{}.owner_before(weak); } This will only return true if w{} “==” weak, where “==” compares owner, and according to en.cppreference.com: The order is such … Read more
So shared ptr and weak ptr are thread safe, in that if you have an instance of the object local to a given thread, and they share a common pointed-to object, you can interact with them in one thread and another and everything works. For this to work properly, you have to use them properly. … Read more
Completely rewriting this answer because I totally misunderstood. This is a tricky thing to get right! The usual implementation of std::weak_ptr and std::shared_ptr that is consistent with the standard is to have two heap objects: the managed object, and a control block. Each shared pointer that refers to the same object contains a pointer to … Read more
The fundamental conceptual difference between a naked pointer and a weak_ptr is that if the object pointed to is destroyed, the naked pointer won’t tell you about it. This is called a dangling pointer: a pointer to an object that doesn’t exist. They’re generally hard to track down. The weak_ptr will. In order to use … Read more
Completely rewriting this answer because I totally misunderstood. This is a tricky thing to get right! The usual implementation of std::weak_ptr and std::shared_ptr that is consistent with the standard is to have two heap objects: the managed object, and a control block. Each shared pointer that refers to the same object contains a pointer to … Read more
In general and summary, Strong pointers guarantee their own validity. Use them, for example, when: You own the object being pointed at; you create it and destroy it You do not have defined behavior if the object doesn’t exist You need to enforce that the object exists. Weak pointers guarantee knowing their own validity. Use … Read more
shared_ptr uses an extra “counter” object (aka. “shared count” or “control block”) to store the reference count. (BTW: that “counter” object also stores the deleter.) Every shared_ptr and weak_ptr contains a pointer to the actual pointee, and a second pointer to the “counter” object. To implement weak_ptr, the “counter” object stores two different counters: The … Read more
If you think about it, a weak_ptr must refer to something other than the object itself. That’s because the object can cease to exist (when there are no more strong pointers to it) and the weak_ptr still has to refer to something that contains the information that the object no longer exists. With a shared_ptr, … Read more