This pattern is licensed under the CC0 Public Domain Dedication.
Maintain a non-owning reference to a shared dynamically allocated object to break circular dependencies.
std::weak_ptr type represents
a non-owning reference to dynamically allocated object with
shared ownership (
they do not contribute to the reference count of the managed object
they refer to, the object can be destroyed at any time when all
std::shared_ptrs give up ownership. However, a
std::weak_ptr can be converted to a
std::shared_ptr to provide
temporary ownership and safe access to the object.
In the example code, we have two classes:
foo on lines 5–14, and
lines 16–30. A
foo object has shared ownership of a
bar (line 13), and
bar requires some form of reference back to the
foo that owns it.
If this back reference were a
std::shared_ptr<foo>, it would introduce
a circular dependency, making it impossible for either object to be
destroyed. If it were a normal reference type (
foo&), it risks
refering to a deleted object when it attempts to use it, as the
foo is independent of
The solution is to use a
std::weak_ptr<foo>, as on line 33. When
bar needs to use
foo, it checks if bar still exists by calling
lock on the
std::weak_ptr to take temporary shared ownership
(line 26). If the returned
std::shared_ptr is not empty,
safely use it to access the
foo object (lines 27–29).
10 December 2017