3

I am implementing a memory tracker in my application so that further down the line, should I get any memory leaks I can switch this little guy on to find it.

All is great except that I am never passed the filename or the line number. Is there some flag I have to set using _CrtSetDbgFlag, or a preprocessor command?

After I ran the thing (bare-bones) it showed 26 allocations that were not cleaned up and I am pretty sure they are not me, but have no idea where they occurred.

Thanks in advance!

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • Have you considered using [vld](http://sites.google.com/site/dmoulding/vld) instead of rolling your own? – Björn Pollex Nov 14 '10 at 11:39
  • @Space_C0wb0y, thanks for that link I'll check it out. But also this was an exercise in understanding the CRT debugging features (and I hope to capture the stack as well soon), and go "under the hood" to some extent :) – Moo-Juice Nov 14 '10 at 11:45

1 Answers1

4

From the <crtdbg.h> header file:

#ifdef  _CRTDBG_MAP_ALLOC
#define   malloc(s)             _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
// etc...
#endif

Note how the redefinition now calls another version of malloc that has the file and line number that you are looking for. Clearly, to make this work you will have to #define _CRTDBG_MAP_ALLOC and #include crtdb.h. This is best done in your precompiled header file so that you can be reasonably sure that all of your code will be compiled with these macros in effect.

That still doesn't guarantee that you'll get this info. Your project might be using a .lib that was compiled without it. Another failure mode is DLLs that might be unloaded just before you generate the leak report. The file and line info for that DLL will be unloaded as well.

There's a fallback to diagnose those kind of trouble makers. The leak report has a line for the leak that starts with the block number, shown at the start inside curly braces. As long as that block number is stable between runs, you can force the debugger to break when the allocation is made. Put this code in your main method or whatever point in your code that executes early:

 _crtBreakAlloc = 42;   // Change the number
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Hi, thanks for this response. I implemented the macro and include as you've outlined above in to my core header file (which gets precompiled), but still not a single file name gets passed (I checked using breakpoints). Is there anything else I might be missing? (thanks for the informed response also) – Moo-Juice Nov 14 '10 at 13:48
  • Note: In the debugger I see that it is still going through the regular malloc call. – Moo-Juice Nov 14 '10 at 13:51
  • Well, that explains it. Why it doesn't use the redefinition is unguessable from where I sit. Do double-check that _CRTDBG_MAP_ALLOC is #defined and trouble-shoot preprocessor misery by looking at the preprocessor output. – Hans Passant Nov 14 '10 at 14:02
  • I understand it must be hard from where you sit :) In my core header file, I #define _CRTDBG_MAP_ALLOC (I double checked spelling, even going so far as to copy the definition from ``) and then include `` directly after. Anyway, I'll see what I can do, your answer is the correct one and so shall be marked as such :) Thanks for your time. – Moo-Juice Nov 14 '10 at 14:08