Search for memory leaks

I am currently debugging some code by removing or at least detecting memory leaks using Visual Studio 2012 with CrtDbg.

The problem is that as long as the distribution number does not change, tracking the distribution is pretty simple. When the distribution number changes a lot (or is not deterministic), how can I find the highlight point of this leak? Can I at least say which module allocated the memory?

I have the following lines when shutting down the application:

Detected memory leaks!
Dumping objects ->
{2789444} normal block at 0x0000000006103CB0, 32 bytes long.
 Data: < q f            > B8 71 E4 66 00 00 00 00 00 00 00 00 00 00 00 00 
{1269709} normal block at 0x000000000A50C6A0, 1008 bytes long.
 Data: <        )       > 01 00 00 00 0B 00 00 00 29 00 00 00 CD CD CD CD 
...
{2194} normal block at 0x0000000000278060, 16 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
Object dump complete.

The last distribution number, 2194, is reproducible and associated with a static initializer. But other numbers are changing.

Can I use the address to find it? Or is there a simpler solution?

Help would be great.

+4
4

:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
class MemChecker 
{
   friend class foo;
   struct foo 
   {
      HANDLE hLogFile;
      _CrtMemState _ms; 

      foo() 
      {
         hLogFile = CreateFile(TEXT("memory_leaks.txt"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

         _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); // enable file output
         _CrtSetReportFile( _CRT_WARN, hLogFile ); // set file to stdout
         _CrtMemCheckpoint(&_ms); // now forget about objects created before

         // breaks on N-th memory allocation
         // look for this number in report file (in curved brackets)
         //_CrtSetBreakAlloc(1518);
      }
      ~foo() 
      { 
         _CrtMemDumpAllObjectsSince(&_ms); // dump leaks
         CloseHandle(hLogFile);
      }
   };
   static foo obj;
};
MemChecker::foo MemChecker::obj;

, , , .

, ( ). : , , , _CrtSetBreakAlloc (2789444 *), , , , ( ).

_CRTDBG_MODE_FILE, _CRTDBG_MODE_DEBUG, , .

* , {2789444} 0x0000000006103CB0, 32 .

0

. , . .

+1

Debug Diagnostic Tool v2.0, Windows, Microsoft .

, , .

exe , "" → " ", exe.

+1

:

:

Visual Leak Detector
    include
        vld.h
        vld_def.h
    lib
        Win32
            vld.lib
        Win64
            vld.lib
    bin
        Win32
            vld_x86.dll
        Win64
            vld_x64.dll

:

#ifdef _DEBUG_MEM
#include <vld.h>
#endif

Add the following parameters to the project settings:

_DEBUG_MEM in the preprocessor-definitions
Visual Leak Detector\include in the include-path
Visual Leak Detector\lib\Win<xx> in the library-path
Visual Leak Detector\bin\Win<xx> in the executable-path
0
source

All Articles