Alex Rivera | Logout

How to detect cycles when using shared_ptr

Asked 2009-04-22T07:33:32.777
26

shared_ptr is a reference counting smart pointer in the Boost library.

The problem with reference counting is that it cannot dispose of cycles. I am wondering how one would go about solving this in C++.

Please no suggestions like: "don't make cycles", or "use weak_ptr".

Edit

I don't like suggestions that say to just use a weak_ptr because obviously if you know you will create a cycle, then you wouldn't have a problem. You also cannot know you will have a cycle at compile time if you generate shared_ptrs at runtime.

So please, self delete answers that use weak_ptr in them because I specifically asked not to have those kind of answers...

Edit
Report

2 Answers

28

shared_ptr represents ownership relation. While weak_ptr represents awareness. Having several objects owning each other means you have problems with architecture, which is solved by changing one or more own's into aware of's (that is, weak_ptr's).

I don't get why suggesting weak_ptr is considered useless.

answered 2009-04-22T11:49:06.033
1

See this post on detecting cycles in a graph.

answered 2009-04-22T14:09:51.873

Your Answer