none
Memory leaks in C++ RRS feed

  • General discussion

  • Hi!
    Created application is working toooo slow, looks like there are a lot of memory leaks, there are a lot of pointers. So, please, can you advice some effective tool for run-time errors and memory leaks detection in Visual Studio C++?

    Monday, January 18, 2010 11:54 AM

All replies

  • Visual Studio Debugger
    Memory Leak Detection and Isolation
    http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx

    Visual Studio Debugger
    Memory Leak Detection Enabling
    http://msdn.microsoft.com/en-us/library/e5ewb1h3.aspx

    Visual Studio Debugger
    The Debug Heap from C++
    http://msdn.microsoft.com/en-us/library/19f56tw3.aspx

    Visual Leak Detector - Enhanced Memory Leak Detection for Visual C++
    http://www.codeproject.com/KB/applications/visualleakdetector.aspx

    - Wayne
    Monday, January 18, 2010 9:59 PM
  • Thanks, and what about Deleaker ( http://deleaker.com/ ) ?

    One friend advised me it recently.

    Monday, January 25, 2010 5:36 PM
  • It looks like BoundsChecker to me, but then again...its supposed to do the same thing, so...that's hardly surprising. Probably, it is also a good tool.

    I mentioned BoundsChecker because I used it many years ago in Win16 MFC development to check for leaks. At the time, MFC CString was pretty leaky, so it helped a lot with developing best practices with respect to using MFC classes and releasing GDI resources.
    Monday, January 25, 2010 5:40 PM
  • Hi,
    Just have a look.
    Its a good practise if we don't depend on IDE/third-party tools/software to detect memory leaks in our code. Its better if we can predict before hand the consequences of writing each line of code.  
     
    But there may be situations where we need to work on projects written by someone else. In such cases these third-party tools may help us in finding a QUICK solution.
     
    The visual studio debugger along with C run-time (CRT) libraries provides us means for detecting memory leaks.But its capabilities are minimal.
     
    Enabling memory leak detection in visual studio:

    1. If our program can exit from multiple locations, instead of putting a call to_CrtDumpMemoryLeaks at each possible exit,include the following call at the beginning of program:
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    This statement automatically calls _CrtDumpMemoryLeaks when program exits.

    2 . The macro  _CRTDBG_MAP_ALLOC applies to c library functions malloc, realloc.With a little bit of extra code, we can get it to work with new/delete:

    #define _CRTDBG_MAP_ALLOC

    #include <iostream>
    #include <crtdbg.h>

    #ifdef _DEBUG
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
    #endif

    These macros should be added after including all other header files.

    3. By default, _CrtDumpMemoryLeaks dumps memory leak information to the Output window,we can reset this to a file using _CrtSetReportMode. For doing this add the following lines of code at the starting of your program.

     HANDLE hLogFile;
     hLogFile = CreateFileA("c:\\memoryLeaksDump.rtf", GENERIC_WRITE,FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);

     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
     _CrtSetReportFile(_CRT_WARN, hLogFile);
     _CrtSetReportFile(_CRT_ERROR, hLogFile);
     _CrtSetReportFile(_CRT_ASSERT, hLogFile);

     _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    thanks

    Eliza


    Cheers, Eliza
    Friday, January 29, 2010 9:36 AM
  • UMDH and LeakDiag are probably the best things to use.
    Friday, January 29, 2010 11:18 AM
  • Its a good practise if we don't depend on IDE/third-party tools/software to detect memory leaks in our code. Its better if we can predict before hand the consequences of writing each line of code.  
     
    But there may be situations where we need to work on projects written by someone else. In such cases these third-party tools may help us in finding a QUICK solution.
     
    The visual studio debugger along with C run-time (CRT) libraries provides us means for detecting memory leaks.But its capabilities are minimal.
     
    Enabling memory leak detection in visual studio:

    1. If our program can exit from multiple locations, instead of putting a call to_CrtDumpMemoryLeaks at each possible exit,include the following call at the beginning of program:
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    This statement automatically calls _CrtDumpMemoryLeaks when program exits.

    2 . The macro  _CRTDBG_MAP_ALLOC applies to c library functions malloc, realloc.With a little bit of extra code, we can get it to work with new/delete:

    #define _CRTDBG_MAP_ALLOC

    #include <iostream>
    #include <crtdbg.h>

    #ifdef _DEBUG
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
    #endif

    These macros should be added after including all other header files.

    3. By default, _CrtDumpMemoryLeaks dumps memory leak information to the Output window,we can reset this to a file using _CrtSetReportMode. For doing this add the following lines of code at the starting of your program.

     HANDLE hLogFile;
     hLogFile = CreateFileA("c:\\memoryLeaksDump.rtf", GENERIC_WRITE,FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);

     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
     _CrtSetReportFile(_CRT_WARN, hLogFile);
     _CrtSetReportFile(_CRT_ERROR, hLogFile);
     _CrtSetReportFile(_CRT_ASSERT, hLogFile);

     _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);


    Cheers, Eliza
    Friday, February 26, 2010 1:30 PM
  • Except _CrtDumpMemoryLeaks is misnamed. It would be better to call it _CrtDumpUsedMemory since when you call it it will detect any memory in use on the CRT heap at that time as a leak.

    Another issue is that if you use HeapAlloc directly or use GlobalAlloc/LocalAlloc, VirtualAlloc or some other custom allocators then it will not detect possible leaks with memory allocated by these functions. _CrtDumpMemoryLeaks only checks the CRT heap, so any other allocated memory is not checked.

    Quite often just knowing what you are doing and getting into good practice for allocation/deallocation is one of the best hings you can do to help get rid of memory leaks.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    Friday, February 26, 2010 6:53 PM