Class Foo {
int id;
GraphNodePtr<Foo> next //defaultly null
Foo(int theID):id(theID){}
};
GraphNodePtr<Foo> graph(0);
{
GraphNodePtr<Foo> node1(1);
GraphNodePtr<Foo> node2(2);
GraphNodePtr<Foo> node3(3);
node1->next = node2;
node2->next = node3;
node3->next = node1; //circular loop
graph->next = node1;
}
graph.next.release()//nodes 1, 2, and 3 are immediately deleted here
That is, you have a a single pointer type that can detect orphaned loops. And the garbage collection needs to guarantee that orphaned objects are released once they are orphaned.