Visual Studio 2008 (C++) memory leak detection not showing file/method location - how to get that to work?

后端 未结 2 880
死守一世寂寞
死守一世寂寞 2020-12-28 22:12

I am using the instructions found here to try to find memory leaks in a Win32 application. As described, I put the

#define _CRTDBG_MAP_ALLOC
#include 

        
2条回答
  •  萌比男神i
    2020-12-28 22:45

    Are you sure the code that's leaking is using the CRT debug allocation routines? That requires using malloc() or new (as opposed to LocalAlloc, GlobalAlloc, some custom block allocator, etc..) and that _DEBUG (I think) must be defined when the CRT headers were included.

    In order to get source lines for leaks, you will need to define DEBUG_NEW everywhere the allocations occur. This is because in order to track them, each allocation must be replaced with a call that includes __FILE__ and __LINE__. The standard definition from the wizard looks something like:

    #ifdef _DEBUG
    #define _CRTDBG_MAP_ALLOC
    #include 
    #include 
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
    #endif
    

    This doesn't handle malloc, there's probably a similar incantation for that, if the code you're debugging uses malloc instead of new.

    If you're using pre-compiled headers, you can just put this in the precompiled header file and it will affect all the source files in that project.

提交回复
热议问题