开发者

memoryleak detection

开发者 https://www.devze.com 2023-01-17 12:59 出处:网络
hey i am trying to detect leaks in visual studio using : #define _CRTDBG_MAPALLOC #include <stdlib.h>

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.


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.


You should use _CRTDBG_MAP_ALLOC and not _CRTDBG_MAPALLOC. The problem is on the MSDN page, where they had a typo, and they talk about both flags, but only the first one is good. If you wish you can inspect crt/crtdbg.h, and you will see it uses only _CRTDBG_MAP_ALLOC.

The MSDN page with the typo is: http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=VS.80%29.aspx


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

There are a couple of ways to accomplish something like this. One way would be 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.

Another way would be to use smarter debuggers that understand the heap allocation path and that track every allocation for you, as Aaron Klotz's documents in his answer.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号