Thread: Memory leaks!!

  1. #1
    Unregistered
    Guest

    Memory leaks!!

    Is there a way I can safely manage all memory leaks? I mean what should I have in mind before the program closes? What should i release / delete?

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Anything allocated with new is a good place to start. Also make sure you dont allocate space more than once for a pointer, eg in a constructor and in a function. I would also look at 3rd party libraries & functions you are might be using, sometimes there is a deleteObject type function to clean things up when you're done.

    There are probably other areas but I'm still thinking...

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Free all memory that you explicitly allocated with new, malloc, or realloc.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    Ok, thanks. Also, is there a program which can measure the memory available before and after running a program?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    void GlobalMemoryStatus(MEMORYSTATUS *);

    The members of the MEMORYSTATUS structure are:

    DWORD dwLength
    DWORD dwTotalPhys
    DWORD dwAvailPhys
    DWORD dwTotalVirtual
    DWORD dwAvailVirtual
    DWORD dwMemoryLoad
    DWORD dwTotalPageFile
    DWORD dwAvailPageFile

    Just call it in a loop and examine the contents each iteration.

    #include <windows.h>
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Perhaps, but I don't know if that is useful. When you start the program which must count the memory, this program will also take memory. And there are probably other processes taking and freeing memory.

    But you can use a tool like Lint to check your code for memory leaks. Such a tool can be found for example here:

    http://lclint.cs.virginia.edu/

    A C++ code memory leak tracer can be found here:

    http://www.andreasen.org/LeakTracer/

    And ofcourse be defensive. Do a lot of error-checking, take care of your pointers, take care of writing beyond array ranges etc. Especially when a pointer is passed to a function.

    Code:
    void function (int *ptr)
    {
        // If ptr points to an array, take care of its length.
    }
    Also take care of more than one malloc in one function:

    Code:
    a = malloc (sizeof (int) * 10);
    
    if (a == NULL)
        return FALSE;
    
    b = malloc (sizeof (int) * 20);
    
    if (b == NULL)
    {
        // Remember here that memory for a is already allocated
        // and should be freed before returning return value
        free (a);
        return FALSE;
    }
    There are lots of books on safe coding, like:

    Safer C
    "Developing for High-Integrity and Safety-Critical Systems"
    ISBN 0-07-707640-0
    McGraw-Hill, 1995
    Les Hatton
    Last edited by Shiro; 05-12-2002 at 03:57 AM.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    If you got MSVC++ there is a good lib u can use:

    [code]
    #ifndef NDEBUG
    //------------------------------------------------------------------------------
    // This code is to turn on and of memleak tracer.
    // Will only be active in debug mode

    #include <crtdbg.h>

    //------------------------------------------------------------------------------
    #endif

    int main()
    {
    #ifndef NDEBUG
    //------------------------------------------------------------------------------
    // This code is to turn on and of memleak tracer.
    // Will only be active in debug mode

    int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag
    flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
    _CrtSetDbgFlag(flag); // Set flag to the new value

    //
    //------------------------------------------------------------------------------
    #endif

    char *pCh = new char[100];
    return 0;
    }

    now you will get info in the debug window telling you there is memory leeks. This way you can always fix the errors the moment they arrise.

    What I also want to suggest when fighting memleeks is a coding style where you always write the deallocation code after you have written the allocation code. Then write the code that is between. That way you won't forget you allocated memory in that nested loop 3 pages up. This method might not be as easy to addopt if one function allocates the memory and another deletes it. But that you shouldn't do anyway.

    Well I could go on forever... hope this helped...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for memory leaks
    By Bladactania in forum C Programming
    Replies: 5
    Last Post: 02-10-2009, 12:58 PM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Tons of memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2005, 10:19 AM
  4. COM Memory Leaks
    By subdene in forum Windows Programming
    Replies: 0
    Last Post: 06-07-2004, 11:57 AM
  5. about memory leaks with this simple program
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2002, 07:19 PM