title: Coding for ease of debugging
body: <p>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:</p>
  
  <pre><code>class MyClass {
      TRACK_THIS_TYPE(MyClass);
  };
  
  boost::shared_ptr&lt;MyClass&gt; myObj(new MyClass);
  TRACK_THIS_OBJECT(myObj);
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Another example would be the old</p>
  
  <pre><code>assert(condition &amp;&amp; "My descriptive text");
  </code></pre>
  
  <p>that allows you to put meaningful comments directly into your assert.</p>
  
  <p>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.</p>
  
  <p><strong>Edit:</strong> In addition to the replies to this question, I've received tips about <a href="http://code.google.com/p/google-glog/" rel="nofollow noreferrer">google-glog</a> as a logging utility.</p>