Search code examples
c++memory-leaksmemory-leak-detector

Not able to see memory leak on console


i am using Visual studio 2010, i tried using CRT library provided by VC for memory leaks. but i am not able to see memory leaks printout on Console. codebase:

#include <iostream>
#include <vector>

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

using namespace std;

int main( )   
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
int Y = 1;
int X = 2;
int** superevil = new int*[Y];
for(int i = 0; i < Y; ++i)
    superevil[i] = new int[X];

superevil[0][2] = 1;

/*for(int i = 0; i < Y; ++i)
  delete[] superevil[i];
delete[] superevil;*/

 _CrtDumpMemoryLeaks();
return 0;
}

can't get the reason.


Solution

  • Note that if you've set _CRTDBG_LEAK_CHECK_DF then you don't need to call _CrtDumpMemoryLeaks() too because it gets called automatically for you at the end of the program. In fact, at the place you called _CrtDumpMemoryLeaks(), no leak had yet occurred.

    Also, this only works in Debug builds run through the IDE and the output - If any- is dumped to the Output window in Visual Studio, not to the console.

    If you remove the call to _CrtDumpMemoryLeaks() and run through the IDE, you will see something similar to the following (I used VS2012) :

    Detected memory leaks!
    Dumping objects ->
    c:\consoleapplication1.cpp(24) : {190} normal block at 0x004CCAF0, 8 bytes long.
    Data: < > CD CD CD CD CD CD CD CD
    c:\consoleapplication1.cpp(22) : {189} normal block at 0x004CCAB0, 4 bytes long.
    Data: < L > F0 CA 4C 00
    Object dump complete.