ref cycles in c++

09/12/2025
Go back

Nothing particularly revolutionary, but an interesting way to break shared_ptr semantics.

#include #include using namespace std; void f() { cout << "Entered f" << endl; struct Cycle { shared_ptr internal_p; ~Cycle() { cout << "Destructed cycle object" << endl; } }; shared_ptr cycle_p{new Cycle{}}; // cycle_p->internal_p = cycle_p; cout << "Exiting f" << endl; } int main() { f(); cout << "Exiting main" << endl; }

Uncommenting the line is how the break occurs.