Years ago, before auto_ptr
, I wrote my own smart pointer class which we used in our codebase extensively. Of course, it was chock full of bugs.
I had exactly the same problem. While I can't show you the actual code I used to identify the call sites where resource leaks originated (because the code no longer exists), I can tell you what I did.
The first thing was I wrote a #define
macro >SHUDDER<, similar to the one I outline here, which I could use in place of new
to allocate my objects. The macro sent additional parameters to the smart pointer, indicating file & line number of the call site. The smart pointer would retain this.
Then I replaced the global new
with my macro when I uttered a magical incantation of #defines
turning this functionality on. Obviously, this should be disabled in all Releasse builds, and only used in Debug builds when you're actually debugging this.
Then I let my program run until I got to a point where I wanted to see all the call sites. I'd execute a dump_call_sites()
method on the smart pointer there, which dumped the static
vector
of call site strings to the stdout. Problem hence found, I'd revert all those magical incantations to turn off all this hokus pokus.
This is truly hack-n-slash codiing. It' far from elegant, and it introduces it's own set of problems. But along with printf
debugging, it has its place. If you don't want to or can't go to the effort of loading up your Rational Purify or Bounds Checker type product, this could help you quickly identify where your leaks are coming from.