Tracer Library
This is a handy little utility for tracing object leaks. Object leaks are like memory leaks except instead of tracking raw memory blocks you track how many objects of a given type have been created. What could be simpler?
To use it, just include a line like TRACER(CLASSNAME) for each class. So for example:
#include "tracer.h"
struct Sprite {
TRACER(Sprite);
// rest of your class code goes here
//...
}
What this gives you is the ability to query how many of each objects are allocated any any given time. From your main () routine, you probably want something like this:
tracer_enable ("all"); // you can also enable for only certain classes; e.g., pass in "Sprite"
// other code
// ...
if (user_wants_dump ()) tracer_dump (); // prints to stdout
For Venture the Void, this was invaluable: I created a realtime display of how many objects were live at a given moment and could then fly around, comparing the actual allocation/deallocation with the expected. So I might notice that every time I entered a planet, the game allocated 7 more "drone" AI objects that weren't freed up. At this point, I could choose to trace individual drone types but if memory serves me rightly, I think I just happened to notice that the planet in question had 7 satellites. Voila: memory leak involving satellite AI code.
Also, it's thread-safe so you can use it even if your objects are created/destroyed in threads.
It also uses die.h, which is what I use for error reporting (very, very simple). You could certainly rework it to not use that.
Enjoy!
| Attachment | Size |
|---|---|
| tracer.h | 741 bytes |
| tracer.cpp | 2.19 KB |
| die.h | 745 bytes |


