I am looking for tips on how to aid my debugging by adding code to my application. An example so that it becomes more clear what I'm after: in order to detect dangling objects held by shared_ptrs I have created a tracker class that allows me to keep track of how many objects are alive and where they where originally created, which is then used like this:
class MyClass {
TRACK_THIS_TYPE(MyClass);
};
boost::shared_ptr<MyClass> myObj(new MyClass);
TRACK_THIS_OBJECT(myObj);
where TRACK_THIS_TYPE(t) is a macro that makes sure that I get an instance count (and count of how many objects have been created) for a class and TRACK_THIS_OBJECT is a macro that stores the file and line where the object was created together with a weak_ptr to the object.
This allows me to detect dangling objects and where they where created. It does not allow me to find out what objects are holding a shared_ptr to my objects, which could be an improvement to the above. I guess one could create something like a TRACK_THIS_PTR(T) macro that would store the file and line for where the shared_ptr instance is created.
Another example would be the old
assert(condition && "My descriptive text");
that allows you to put meaningful comments directly into your assert.
Does anyone have any neat c++ tricks to collect stats, automatic stack traces, tracking of objects/pointers/resources, deadlock/starvation or other threading issues detection, making sure that exceptions get handled somewhere, documentation help or similar? Anything goes really, whether it is something that helps prevent errors or something that helps after the fact.
Edit: In addition to the replies to this question, I've received tips about google-glog as a logging utility.