What this thread does
Do first: declare the child's back-reference as std::weak_ptr<Parent> parent and keep the parent's child link owning only when the parent is responsible for the child's lifetime.
Short answer
As of November 2026, this uses current C++ library documentation, including Microsoft's weak_ptr reference, and the current working-draft weak-pointer contract. Two shared owners pointing back at each other keep their ownership counts above zero after external owners leave. A weak edge breaks that ownership cycle without removing the navigable relationship; the ownership-cycle question describes this still-current problem.
Choose / don't choose
- Parent owns child: choose a shared child link only if ownership really is shared; make the child's parent link weak. Don't make both directions owning by default.
- Peer graph: choose enough weak edges to break every owning cycle, based on who must outlive whom; don't turn all edges weak and accidentally remove the only owner.
- Access: choose one
lock()result for the whole operation; don't checkexpired()and later assume another promotion must succeed (Microsoft reference, atomic promotion).
Not for: Python dependency retention after failed async runs; see pydantic-ai-failed-run-deps-retention.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
weak_ptr | Weak pointers do not affect the resource reference count; constructed from an owning shared_ptr | Current C++ library docs | weak_ptr |
std::shared_ptr<Child> | Retain on the ownership direction when the child has shared owners | Current C++ library docs | cycle example |
lock() | Returns expired() ? shared_ptr<T>() : shared_ptr<T>(*this), executed atomically | Current C++ library docs | observers |
expired() | True when use_count() == 0 | Current C++ library docs | observers |
reset() | Release this weak reference; cannot destroy an object still owned elsewhere | Current C++ library docs | reset |
Use the returned owner, not the weak pointer itself:
if (auto owner = parent.lock()) {
owner->notify();
} else {
return;
}
notify() represents the application operation. An expired weak pointer returns shared_ptr<T>(); skip, return or report that application condition. Do not dereference it or try to resurrect the object (lock contract).
Order of operations
1. Draw the owning edges and remove ownership from the chosen back-link. Assign the weak reference from the existing shared owner; preserve the actual lifetime authority (cycle reference). 2. Release external owners. lock() converts a weak pointer to a shared pointer, so keep its scope bounded. When the final shared owner disappears, the managed object's deleter/destructor runs (shared_ptr destructor). 3. Outstanding weak pointers keep the control block alive. When the shared count decrements to zero, the managed object is destroyed. Destructing a nonempty weak pointer decrements the control-block reference counter; reaching zero deletes the control block. Raymond Chen's August 14, 2023 implementation explanation distinguishes these two lifetimes; internal counters/layout are implementation details.
Known failures and fixes
| Error or symptom | Cause | Fix | Source |
|---|---|---|---|
| Reference counts stay above zero after external pointers are destroyed | Shared ownership still forms a cycle | Replace a non-owning relationship in each cycle with a weak edge | Microsoft cycle example, 2011 report |
| lock returns an empty shared_ptr | The final owner already released the managed object, or the weak reference is empty | Branch on the returned shared_ptr; do not dereference an empty result | lock |
Missing
- These conditions do not require an exception or fixed error string. Weak pointers solve lifetime observation, not concurrent mutation of the pointed-to data. This card does not prescribe one ownership direction for every graph.
Checked
2026-11-12; current weak/shared-pointer contracts, dated Microsoft reference, Raymond Chen's dated implementation explanation and February 2011 question read. No C++ program compiled or executed.
Started from a post by @Microsoft, packaged by the editor. Original post