_CRTDBG_MAP_ALLOC and _CrtDumpMemoryLeaks()

For memory leak detection, I am following the instructions given here:

http://www.codeguru.com/forum/showthread.php?t=312742

...but I am finding that even the simplest console app code is shown to have memory leaks, like this:

int _tmain(int argc, _TCHAR* argv[]){
std::string foo("hello");

_CrtDumpMemoryLeaks();

return 0;
}

The IDE output window says "detected memory leaks" and shows the foo string as leaking. It should never have identified the "hello" string as leaked. And it doesn't tell me which line of code has the problem.

Then I change the "string foo" into an allocation of an integer from the heap using new. This time I expect to be informed of a memory leak and I am. However, instead of telling me the *real* line of code that has the problem, it tells me the problem is here:

c:\program files (x86)\microsoft visual studio 8\vc\include\crtdbg.h(1147) : {121} normal block at 0x01BC6068, 4 bytes long

What am I doing wrong?

[1521 byte] By [Mystagogue] at [2007-12-24]
# 1

In my opinion since your string variable was not yet destroyed, it is reported as a memory leak. In your case this is a false positive error, because the memory block allocated by the string will be correctly de-allocated later after finishing the main function.

I think if you put your variable in a separate block and limit it’s existence, like this:

int _tmain(int argc, _TCHAR* argv[])

{

{

std::string foo("hello");

}

_CrtDumpMemoryLeaks();

return 0;

}

then no more problems will be reported. The destructor of your string was already called before _CrtDumpMemoryLeaks.

Therefore you should execute the _CrtDumpMemoryLeaks function when other your code was already finished. Probably you should put your code in a separate function and call it before _CrtDumpMemoryLeaks.

I hope this help.

Viorel. at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ General...