I am trying to detect memory leak using _CrtDumpMemoryLeaks in Visual Studio 2008. The problem is that it reports the position (file and line number) of some memory leaks, but not all of them.
I already include the following code in all the files in the project, so it seems very weird, since it is working "partially".
If memory is allocated in code without debug information, such as code within a library, there can be no file and line to report, because that information doesn't exist.
The leak detection at the end of the program shows the allocation number. If your leak is at the same allocation for each run, you can break into the program when the allocation is being made.
Thanks guys, it helps me to understand the problem better.
kbw: Do you mean to set _crtBreakAlloc to be the allocation number I want to identify? It indeed breaks when the allocation number is reached, but still does not provide any location information.
If it breaks in the debugger, you'll have a call stack, and you'll see the allocation being made that isn't being freed. In short, you have everything.
as you can see, I comment out the delete command and the memory allocated by new is not freed, so memory leak is detected with the a nice report with location information:
Detected memory leaks!
Dumping objects ->
c:\...\visual studio 2008\projects\memorytest\memorytest\memorytest.cpp(41) : {122} normal block at 0x00336658, 12 bytes long.
Data: < > CD CD CD CD CD CD CD CD 01 00 00 00
c:\...\visual studio 2008\projects\memorytest\memorytest\memorytest.cpp(39) : {121} normal block at 0x00336618, 4 bytes long.
Data: <Xf3 > 58 66 33 00
Object dump complete.
Then I uncomment the delete statements, while I expect no memory leak, I get the report without location information:
You need to call _CrtSetBreakAlloc(nnn); nnn is the allocation number that you want the app to stop at. You get this from the list of unfreed blocks at the end of the program, 120 in the example above.