CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005

    Where should I put _CrtDumpMemoryLeaks in the code?

    To enable memory leaks detection, where should I put _CrtDumpMemoryLeaks in the code? For example, in the program I define a class A. A member function Load is defined in class A. The function Load is used to allocate memory for some variables in the heap and then at the end of pragram, the momory is deallocated in the destructor of class A. In this scenario, where should I put _CrtDumpMemoryLeaks in the code? Thanks.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    Re: Where should I put _CrtDumpMemoryLeaks in the code?

    How about looking in msdn first before posting to CG?

    see the sample project listed in the link above

    look at sample code, see where the sample puts the _CrtDumpMemoryLeaks calls.

  3. #3
    Join Date
    Oct 2006

    Re: Where should I put _CrtDumpMemoryLeaks in the code?

    You can also add
    #ifdef _DEBUG
       #define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
       #define DEBUG_CLIENTBLOCK
    to a header that's included everywhere (stdafx.h perhaps) and
    #ifdef _DEBUG
      #define new DEBUG_CLIENTBLOCK
    as early as possible in every cpp-file and in main
    int main()
    #if defined(_DEBUG)
      int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
      tmpFlag    |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF;  // Check heap alloc and dump mem leaks at exit
      _CrtSetDbgFlag( tmpFlag );
      assert( !errno );
      //_crtBreakAlloc = 58652; // Set to break on allocation number in case you get a leak without a line number
    This will make every single leak to be detected regardless where it occur
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here

    Get your free MS compiler here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.