Reputation: 259
I'm currently in debuging some code, removing or at least locating memory leaks using Visual Studio 2012 with CrtDbg.
The problem is, as long as the allocation number does not change, tracking down the allocation is rather easy. When the allocation number changes a lot (or is not really deterministic), how can I locate the allocation point of that leak? Can I a least say, which module was allocating the memory?
I have following lines on shutdown of 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 allocation number, 2194, is reproducable and is related to a static initializer. But the other numbers are changing.
Can't I use the address to locate it? Or is there a simpler solution to it?
Help would be great.
Upvotes: 4
Views: 1207
Reputation: 707
You can use these technique:
#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;
with this declaration, every time you run your programm memory leaks will be detected and reported in a proper way.
Also, you can set breakpoint to a particularly memory leak (read the comments in code). How to do that: you run application, see, what the number of memory leak, then you set _CrtSetBreakAlloc(2789444*), so next time you run the app - it breaks on the place, where the memory (that leaked) was allocated.
You can read more carefully about flags _CRTDBG_MODE_FILE, _CRTDBG_MODE_DEBUG, to specify the place, where debug messages will be outputed.
*from your example, {2789444} normal block at 0x0000000006103CB0, 32 bytes long.
Upvotes: 0
Reputation: 6050
Try the Debug Diagnostic Tool v2.0, it is a very good memory dector tool on Windows, it is from Microsoft and it is free.
If the 3rd party libraries leaks memory, the tool can locate the library, just without the call stack information.
To start the exe via this debugger, go to the menu 'Tools'->"Pre-Attach Configuration', enable pre attach debugger for your exe.
Upvotes: 1
Reputation: 30126
Create the following directory structure:
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
Add the following above your main function:
#ifdef _DEBUG_MEM
#include <vld.h>
#endif
Add the following in your 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
Upvotes: 0