Memory Leak and New


  • I'm a bit confused about the utility of crtdbg.h if I use the new function.

    If I use malloc I get a nice report saying that line x of file y leaked z amount of memory. Nice.

    If I use new, I get a pretty useless report that line 691 of crtdbg.h leaked z amount of memory. Well gee, thanks. I KNEW that new is being redirected to that inline function in crtdbg.h, I was kinda hoping maybe you could tell me where I came from, not where I went.

    Here is the source:

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

    int main(int argc, char* argv[])

    int* leak = new( _NORMAL_BLOCK, __FILE__, __LINE__ ) int[50];
    int* useless_leak = new int[10];

     int* leak2 = (int*) malloc( sizeof(int) *25 );

    return 0;

    Here is my slightly useless report:
    Detected memory leaks!
    Dumping objects ->
    .\fixed.cpp(76) : {91} normal block at 0x00891058, 100 bytes long.
     Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
    C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\crtdbg.h(692) : {90} normal block at 0x00890FF0, 40 bytes long.
     Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
    .\fixed.cpp(73) : {89} normal block at 0x00894F20, 200 bytes long.
     Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
    Object dump complete.

    As you can see, malloc works fine. If I explicitly call the new operator from the crtdbg.h file, I get a useful report. However, if I just all new, I get crtdbg.h.  Even after I hacked crtdbg.h to use __forceinline so that I was sure that the function WAS being inlined, it doesn't matter. Apparently __LINE__ and __FILE__ aren't so useful for inline functions.

    I'm using .NET 2003. Anyone have any ideas? Do I need to flip an obscure switch that I couldn't find in the documentation? Am I simply an idiot? Did MS not test the new operator at all?

    Larry E. Ramey

    Friday, February 17, 2006 3:18 PM


All replies

  • after using malloc() use must use free(void *) to free the memory again or else it is considered a leak.  And with new you must use delete  to free the memory again or else you have a memory leak again .

    For example in your code use:
    Friday, February 17, 2006 5:16 PM
  • I am exceedingly aware of what a leak is.

    My question, if you re-read it, was not about what a memeory leak is. I am curious as to why the information comming out of debug new operator defined in crtdbg.h was so useless.

    I would be very appriciative if anyone with a clue about the _Crt*** functions could explain to me how to get a useful file and line number out of new without having to re-write my 1,000,000 line project.

    Larry E. Ramey
    Friday, February 17, 2006 5:37 PM
  • I think you have to create a special macro:

    #include "crtdbg.h"
    #define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_CLIENTBLOCK


    Tuesday, February 21, 2006 4:19 PM
  • This issue has been nicely documented by Roshan James.  See the article, "Another Tale of Real World Debugging" online at:

    I am seeing the problem with "Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86" from the "Visual C++ 2005 Express Edition" distribution.  Obviously this is a bug in the compiler that needs to be fixed by Microsoft.

    How do we get Microsoft's attention on this issue?  Can I see if a bug report is open for this or submit a bug so that Microsoft will be aware of and fix the problem?

    Monday, May 22, 2006 4:31 PM
  • I suppose there's no harm in logging a bug at However, I'm not sure how Microsoft can fix this issue without breaking code that relies on the workaround.

    Monday, May 22, 2006 6:53 PM
  • Thanks for the link to the bug reports.  I find this issue is already addressed by bug FDBK17590, which is unfortunately marked as postponed. This is suprising to me as memory leak detection is such a fundamental part of C/C++ development.

    Monday, May 22, 2006 8:55 PM
  • i think the issue was addressed, the _crtDumpMemoryLeaks(); shows the line in cpp file where the  leakl is happening


    Friday, June 09, 2006 4:04 AM