#include #include struct Node { static int alive; std::shared_ptr child; std::weak_ptr parent; Node() { ++alive; } ~Node() { --alive; } }; int Node::alive = 0; int main() { auto parent = std::make_shared(); auto child = std::make_shared(); parent->child = child; child->parent = parent; { auto locked = child->parent.lock(); assert(locked && locked->child == child); } parent.reset(); assert(Node::alive == 1); assert(child->parent.expired()); assert(!child->parent.lock()); child.reset(); assert(Node::alive == 0); }