Boost logo

Boost Users :

From: Johan Råde (rade_at_[hidden])
Date: 2006-11-15 08:20:01


bringiton bringiton wrote:
> Can anyone offer advice on detecting circular references with shared_ptrs.
>
> I have a large app, 50+ classes,

That is not large. I'm working on an app with 500+ classes.
One of my friends is working on an app with 5000+ classes.

> and most objects are managed with shared_ptrs.
> No consideration has gone into avoiding circular references,
> so I'm sure there will be a few in there.

[snip]

Sounds like you have created quite a mess :-)
Some serious refactoring is needed,
instead of just patching up what you got.

The key to effective object life time management
is to have an acyclic object ownership graph.
Then you use shared pointers when going down,
weak pointers when going sideways,
and weak pointers (or sometimes plain pointers) when going up the graph.

This should be done at the design stage,
and not when testing and debugging.
Draw UML diagrams of your app,
and use different kind of arrows for different kinds of pointers.
(I use solid diamond arrows for scoped pointers,
empty diamond arrows for shared pointers,
and plain arrows for weak and plain pointers.)

The best way to get an acyclic ownership graph
is to have an acyclic dependency graph.
So you should really start with the dependency graph.
In practice you can never get it completely acyclic,
but often you can get quite close.
There are many techniques for doing that.
There is in fact a whole book on the subject:
Large-Scale C++ Software Design by John Lakos.

To the list of techniques in that book,
I would like to add one more:
Boost.Signals is very powerful tool for eliminating object dependencies.

Maybe your manager tells you that you don't have time to do this.
Well, you will waste more time not doing it.

--Johan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net