you didn't specify which platform you are looking for. I'm a windows developer so I can only recommend windows solutions. If that's what you are working with, there's a number of commercial and free tools available. Few that I personally worked with: Purify, BoundsChecker, UMDH, LeakDiag, DebugDiag.
Out of these, I typically prefer UMDH. It is free and comes as part of Debugging Tools for Windows (DTW) installation. I found it to actually be more reliable and less resource intensive than most other, including professional tools. It is very simply to use and documentation can be found in a .chm file which comes with DTW install. In the end, I personally find that UMDH has very high signal to noise ratio compared to many other tools.
DebugDiag is another good alternative. As far as I can tell, it uses almost same APIs as UMDH but is slightly more cumbersome to use because it's UI-based rather than command prompt so to get things done usually requires more clicks, but for someone new, I would recommend it over UMDH.
UPDATE:
It's interesting that majority preference was to insert custom hooks into malloc/free and then add even more code for custom hooks into operators new/delete.
I would strongly suggest you take a look at UMDH and learn how it works even if you don't find in necessary in this specific case. At the core of all memory allocations are windows functions, HeapAlloc/HeapFree. Microsoft, anticipating the need for leak detection methods, already provided hooks that we can use at that root level.
These are other advantages of using UMDH over custom allocator hooks:
- You get full stack trace of each allocation instead of just what's provided by __FILE__ and __LINE__
- It already has full reporting and aggregation of statistics which is something you'd have to write on top of just intercepting malloc/free. You get # of allocations for each trace, # of bytes allocated by each trace and a list of allocated memory buffers so you can actually analyze what kind of data was leaked.
- Detecting malloc/free leaks which occurred in someone else's code, not specifically the DLL under your control
- Detecting leaks from other memory allocation functions such as CoTaskMemAlloc or SysStringAlloc
- Detecting leaks of COM objects which are not properly released
- Detecting logic errors in your code when you call into third party API that returns a buffer which you forgot to free.
- You can use UMDH instantly with any code base without having to add custom code over and over again.
- The method you accepted only works in debug environment. UMDH can be used just as effectively without any code changes on production systems.
Pretty much, any time there's an upward trend in memory usage, the tool will tell you where it's coming from. Most of the time, I can find a leak within 10 minutes if its reproducible on the development machine (it takes a bit longer when debugging production code because symbol files must be matched, sometimes manually).
So if you get all this completely free with DTW installations (which btw has other awesome debugging features), why do people prefer to roll their own leak detection code?