Where should I put _CrtDumpMemoryLeaks in the code?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Where should I put _CrtDumpMemoryLeaks in the code?

  1. #1
    Join Date
    Jul 2005
    Posts
    966

    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
    Posts
    11,478

    Re: Where should I put _CrtDumpMemoryLeaks in the code?

    How about looking in msdn first before posting to CG?
    http://msdn.microsoft.com/en-us/libr...=VS.80%29.aspx

    see the sample project listed in the link above
    http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx

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

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,642

    Re: Where should I put _CrtDumpMemoryLeaks in the code?

    You can also add
    Code:
    #ifdef _DEBUG
       #define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
    #else
       #define DEBUG_CLIENTBLOCK
    #endif
    to a header that's included everywhere (stdafx.h perhaps) and
    Code:
    #ifdef _DEBUG
      #define new DEBUG_CLIENTBLOCK
    #endif
    as early as possible in every cpp-file and in main
    Code:
    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
    #endif
    ...
    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
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

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

This is a CodeGuru survey question.


Featured


HTML5 Development Center