views:

75

answers:

3

hey i am trying to detect leaks in visual studio using :

#define _CRTDBG_MAPALLOC
#include <stdlib.h>
#include <crtdbg.h>

and in the end of the main i am typing :

_CrtDumpMemoryLeaks(); 

when i do all of this i am getting the the memoryleaks (what inside them) but not the places that the allocates were made , can u please help me with the command that show where were the allocated been made , thanks in advance.

+2  A: 

You can't. CrtDumpMemoryLeaks only tells you if there are any memory leaks, not where the memory leak is. The CRT provides no such facility.

If you wanted something like this, you would need to use a tool like Valgrind, which instruments the entire application and runs the application inside a Virtual Machine. Valgrind severely slows down the application but makes this kind of analysis possible. The CRT doesn't have the luxury of running things in a virtual machine, so it can't really provide information like this.

Oh, one more thing -- if you're using memory correctly in C++, you shouldn't ever be having to worry about memory leaks because you shouldn't be deleteing memory manually. Consider wrapping any calls to new using various smart pointer types instead.

Billy ONeal
i am working on windows enviroment and i cannot use valgrind (only able in linux if i am not wrong).isnt there another technic to know where was the allocation was made?
Nadav Stern
@Nadav: Not as far as I am aware. Your business logic code does compile on multiple platforms though, right? :)
Billy ONeal
it does tell you the sequence number of the allocation though, and you can use `CrtSetBreakAlloc` to break on that allocation the next time you run the app. Of course that only works when the order of allocations until the leak is deterministic.
tenfour
A: 

check this

Chubsdad
+2  A: 

Why not use the UMDH utility that comes with the free Debugging Tools For Windows package from Microsoft? Provided that you have your debugging symbols set up correctly, it will give you the actual call stacks of the allocations.

NOTE: If you're using COM and BSTR, make sure that you set the OANOCACHE environment variable to 1. If you don't, OLEAUT32.DLL will cache BSTR allocations and they will show up as false positives in your UMDH output.

Aaron Klotz
PS make sure you are not using LFH or the allocations are hard to track
Steve Townsend