CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Memory Leaks

  1. #1
    Join Date
    Apr 1999
    Posts
    2

    Memory Leaks

    Does anyone know of any usefull VC macros, addons, anythings that will help me track what objects have been allocated on the heap but not deallocated?
    Regards,


  2. #2
    Join Date
    May 1999
    Location
    Oregon, USA
    Posts
    302

    Re: Memory Leaks

    There are two I use ALL THE TIME. I emphasize this because they are quite
    effective and help you track down memory leaks very well. The first is this :

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif // _DEBUG

    Use this in C++ programs. Check the docs for specific usage instructions.
    I think that it must follow IMPLEMENT_DYNCREATE statements to work correctly.
    The second thing I always do is this :

    #ifdef _DEBUG
    #define _CRTDBG_MAP_ALLOC
    #include <crtdbg.h>
    #endif

    This is for use with standard C run-time library stuff.
    This will help track malloc and free calls.
    Lastly, I use this little function :

    void SetMemDbgFlag( void )
    {
    #ifdef _DEBUG

    int tmp = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
    tmp |= _CRTDBG_LEAK_CHECK_DF;
    tmp &= ~(_CRTDBG_CHECK_CRT_DF );
    _CrtSetDbgFlag( tmp );

    #endif // _DEBUG
    }


    This helps enable memory leak detection. I call this in InitInstance of MFC apps.
    When you use these things, it also pays off to use CGdiObject-derived objects
    for drawing because they work with the memory tracking and their destructors
    will destroy the resource automatically. This helps you track resource leaks.
    Using these techniques I have made my app leak-free so that it runs for months
    at a time with no problems.



Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured