Can anyone offer advice on detecting circular references with shared_ptrs. I have a large app, 50+ classes, and most objects are managed with shared_ptrs. No consideration has gone into avoid circular references, so I'm sure there will be a few in there. I thinking that looking though all the classes and their attributes will take a while. Any suggestions/advice in finding circular references (so i can use weak_ptrs)? I was thinking of maybe getting a tool that draws the class diagram. Or having a base class for all my classes that reference count to a global variable to detect the number of leaks. It seems the shared_ptr is a good idea for memory management, but it opens up the original problem of memory leaks...
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
bringiton bringiton wrote:
Can anyone offer advice on detecting circular references with shared_ptrs.
There is an example in libs/smart_ptr/test/collector_test.cpp that shows how to use libs/smart_ptr/src/sp_collector.cpp to detect (and optionally reclaim) shared_ptr cycles. You have to #define BOOST_SP_ENABLE_DEBUG_HOOKS for your project and add sp_collector.cpp to it.
Hi, I guess the subject says it all. Is there a way to debug templates? with kdbg preferrably? I've read you can with dbx but don?t know if you can with others. How do you do it? Thanks.
participants (4)
-
Berenguer Blasi
-
bringiton bringiton
-
Johan Råde
-
Peter Dimov