_CRTDBG_MAP_ALLOC not showing file name

前端 未结 2 1147
不知归路
不知归路 2021-02-04 02:28

I am trying to detect memory leak, and I am using make _CRTDBG_MAP_ALLOC macro to locate where the leaks area. So I am defining MACRO like following:

#ifdef _DEB         


        
相关标签:
2条回答
  • 2021-02-04 02:32

    It seems the line of the leak only is displayed if the CRT is turned on in that cpp file.

    0 讨论(0)
  • 2021-02-04 02:40

    In my case I ended up including the stuff from this thread into my code. This overrides the new operator and includes the name of the file and the line number into it for later printing. Not sure if this is applicable to Visual Studio only.

    #define _CRTDBG_MAP_ALLOC
    #include <crtdbg.h>
    #ifdef _DEBUG
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
    #endif
    

    The whole test code from the referenced source is:

    #define _CRTDBG_MAP_ALLOC
    #include<iostream>
    #include <crtdbg.h>
    #ifdef _DEBUG
        #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
        #define new DEBUG_NEW
    #endif
    
    int main() 
    {
        char *a = new char[10];
        _CrtDumpMemoryLeaks();
        return 0; 
    }
    

    which in my test case prints:

    Detected memory leaks!
    Dumping objects ->
    e:\test\testapplication\testapplication.cpp(11) : {144} normal block at 0x007F4EF0, 10 bytes long.
     Data: <          > CD CD CD CD CD CD CD CD CD CD 
    Object dump complete.
    
    0 讨论(0)
提交回复
热议问题