24

I am trying to detect memory leak, and I am using make _CRTDBG_MAP_ALLOC macro to locate where the leaks area. So I am defining MACRO like following:

#ifdef _DEBUG
    #define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
#endif

In my code, I have:

UINT SomeFunThread( LPVOID pParam )
{
   _CrtMemState crtMemStateStart;
    _CrtMemState crtMemStateFinish;

    _CrtMemCheckpoint(&crtMemStateStart);


    // My suspisious code


     _CrtMemCheckpoint(&crtMemStateFinish);

      int nDifference(0);
      _CrtMemState crtMemStateDifference;
      nDifference = _CrtMemDifference(&crtMemStateDifference, &crtMemStateStart, &crtMemStateFinish);

    if(nDifference > 0)
        _CrtDumpMemoryLeaks();

    return 0;
}

(Thanks to Tushar Jadhav: Memory consumption increases quickly, then drops very slowly; memory leak?)

But the output shows something like:

Detected memory leaks!
Dumping objects ->
{124058} normal block at 0x0000000031DED080, 24 bytes long.
 Data: < 0      ` $     > C8 30 F7 EF FE 07 00 00 60 D2 24 1D 00 00 00 00 

instead of something like this:

Detected memory leaks!
Dumping objects ->
C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18} 
normal block at 0x00780E80, 64 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.

So how can I make this show the file name and location of the leak?

Community
  • 1
  • 1
Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73
  • The code that allocates just wasn't compiled with your #defines in effect. Could be located in a library, could be in a DLL like the prebuilt C++ template class specializations. Or the DLL that contains the code got unloaded before the leak report was generated. – Hans Passant Nov 20 '13 at 22:18
  • @HansPassant So are you say that it was not my "suspicious code" itself that leaks; it was some code/bianraies/dlls that get called within "suspicious code" ? – Nick X Tsui Nov 21 '13 at 19:24
  • I'd say it is code you haven't found, could be anywhere. The usual problem with leaks, isn't it? If you can get the number between {braces} to repeat then set _crtBreakAlloc to that number. – Hans Passant Nov 21 '13 at 22:40

2 Answers2

12

It seems the line of the leak only is displayed if the CRT is turned on in that cpp file.

DanielV
  • 2,076
  • 2
  • 40
  • 61
Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73
  • 7
    Is there a quick way of turning on CRT in all files without, e.g. creating a new header with the defines in it and including it everywhere? – aquirdturtle Jan 07 '16 at 21:10
  • 1
    Seconding aquirdturtle's question: it looks like all code in my software starts off including "stdafx.h" so I added these three lines after the "TODO" comment, and rebuilt. It wasn't sufficient. `// TODO: reference additional headers your program requires here` `#define _CRTDBG_MAP_ALLOC` `#include ` `#include ` – Swiss Frank Feb 09 '16 at 03:48
11

In my case I ended up including the stuff from this thread into my code. This overrides the new operator and includes the name of the file and the line number into it for later printing. Not sure if this is applicable to Visual Studio only.

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

The whole test code from the referenced source is:

#define _CRTDBG_MAP_ALLOC
#include<iostream>
#include <crtdbg.h>
#ifdef _DEBUG
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
#endif

int main() 
{
    char *a = new char[10];
    _CrtDumpMemoryLeaks();
    return 0; 
}

which in my test case prints:

Detected memory leaks!
Dumping objects ->
e:\test\testapplication\testapplication.cpp(11) : {144} normal block at 0x007F4EF0, 10 bytes long.
 Data: <          > CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
Krøllebølle
  • 2,878
  • 6
  • 54
  • 79