Thread: crtdbg.h and mem leaks

  1. #1
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345

    Question crtdbg.h and mem leaks

    trying to use crtdbg.h to remove memory leaks from my program, I've followed instructions in MSDN here:
    http://216.239.53.100/search?q=cache...hl=en&ie=UTF-8
    It works, but for some reason, instead of getting the filename and the line number where memory allocation happens, it always points to crtdbg.h, so I can get any useful info out of it.
    Code:
    d:\microsoft visual studio .net\vc7\include\crtdbg.h(689) : 
    {198} normal block at 0x1190D4D0, 28 bytes long.
    Am I missing something?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Just a guess, but is this at the top of your cpp file (after the includes)?
    Code:
    #ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif

  3. #3
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    I tried to add those lines, but I got a compiler error, DEBUG_NEW undeclared identifier.
    Where is DEBUG_NEW supposed to be defined? It's not in crtdbg.h

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I'm not sure, it just works for me. What if you just remove the line about DEBUG_NEW?

    I know that in our setup, when the file and line number are not displayed it is almost always because someone forgot to put those lines at the top of the cpp file.

  5. #5
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Thanks for the response

    while going through ctrdbg.h I found:
    Code:
    /* Reports with file/line info */
    
    #define _RPTF0(rptno, msg) \
            _RPT_BASE((rptno, __FILE__, __LINE__, NULL, "%s", msg))
    So I guess it uses __FILE__ ...
    I also searched MSDN for DEBUG_NEW and it turned out to be an MFC thing, so it's not defined because I use Win32 API.

    I tried to add:
    Code:
    #ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #endif
    but I didn't get actual filenames or line numbers
    Anyway, Should I add all these stuff (crtdbg.h defines / your defines) in every .cpp file I have, or it's enough to add it to main.cpp (where WinMain is)?
    I tried to add them to the file where I suspect leaks but I got the same result.
    Thanks in advance.

  6. #6
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Problem solved I found the solution on page 4 after trying every possible keyword combo on Google.

    I needed to add:
    Code:
    #define CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    
    // The most important line
    #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
    to every .cpp file I had.
    Thanks for your efforts

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs and mem allocation
    By reversaflex in forum C Programming
    Replies: 2
    Last Post: 07-03-2008, 07:46 AM
  2. Mem alloc
    By dazellerington in forum C Programming
    Replies: 2
    Last Post: 05-06-2007, 02:55 PM
  3. Replies: 6
    Last Post: 06-02-2006, 08:32 AM
  4. Problems with unsigned char and mem management.
    By Hulag in forum C++ Programming
    Replies: 8
    Last Post: 10-13-2005, 07:03 AM
  5. dmalloc flags unfreed pthread mem
    By rotis23 in forum C Programming
    Replies: 1
    Last Post: 10-09-2002, 05:05 AM