What are the 3 most common reasons to cause memory leak in C/C++?

Posted on 2007-10-08
Last Modified: 2013-12-14
Hi experts,

I know that without calling "free/delete" in C/C++ causes memory leaks. Besides this what are other most common reasons to cause memory leaks in C/C++?

Thanks a lot.
Question by:davidw88
    LVL 4

    Expert Comment

    Some APIs will allocate memory themselves and pass back allocated memory (I can't think of any off the top of my head, but I know I've seen it). In those cases, you have to ensure you free that memory that is passed back.
    LVL 4

    Accepted Solution

    Another one would be not releasing a handle. For example, if you use LoadLibrary to load a DLL, that DLL will not unload until you use FreeLibrary on the DLL's handle returned from LoadLibrary (or until your process unloads, when it will free up just like malloc'd memory would).

    Any handle that you get in Windows usually needs to be closed, such as via CloseHandle(), FreeLibrary(), etc depending on the handle type.

    And I just remembered - a case of an API allocating memory itself that needs freed (from my post above) would be AllocateAndInitializeSid (, which requires that you call FreeSid() on the returned sid, otherwise that will be a leak.
    LVL 30

    Expert Comment

    The following are other functions that can cause memory leaks:



    Check out the following link for other functions that can cause memory leaks and handle leaks:

    LVL 4

    Expert Comment

    GlobalAlloc and LocalAlloc too.
    LVL 4

    Expert Comment

    Plus: GlobalReAlloc/LocalReAlloc and any of the other heap management functions (HeapAlloc, CreateHeap, etc). Additionally VirtualAllocEx, AllocateUserPhysicalPages, etc.

    These are definitely not "top 3", but if you're doing drivers or native apps, you also might have: AllocateCommonBuffer, NtAllocateVirtualMemory, NtAllocateUserPhysicalPages, NtCreateSection, RtlAllocateHeap, RtlAllocateHandle, RtlAllocateAndInitializeSid, RtlDuplicateUnicodeString, etc.

    Essentially, anywhere that memory is allocated, there must be an associated free, and it all boils down to that. All of the functions that have been mentioned above allocate memory in some way. When that memory is not freed, it's a memory leak.


    Expert Comment

    >>Some APIs will allocate memory themselves and pass back allocated memory.
    I just want to add one more point to what jimstar says.
    C++ programmers use many  libraries.Once while I was attending a C++ training, I remember that the trainer was saying like "STL do all the memory manegemnt for us, but it has lots of memory leaks".So they don't use the STL for critical applications like embedded systems and all(I don't know how many people agree to this).
    LVL 12

    Expert Comment

    A little less complicated than the above mentioned but one of the most common that I have seen is where someone declares a NEW pointer but does not unallocate/DELETE the memory associated with it when they are finished.


    LVL 39

    Expert Comment

    >>>> I know that without calling "free/delete" in C/C++ causes memory leaks.

    Actually other reasons are less important.  It is clear, that if calling some 'alloc' function you need to call the  corresponding 'free'. Normally, a good program design should prevent from allocating memory in a function and have to free in another function. In C++ use classes to allocate in the consructor or any 'create' function and delete in the destructor. But of course you need to delete the class object if created on the heap (by 'new') or the destructor won't be called. The thre main reasons for leaks are

    1. forgetting to free/delete
    2. returning without freeing/deleting
    3. assigning a new allocation to a pointer without deleting the old one.

    Some less common reasons:

    4. Overwriting pointers by writing beyond array boundaries
    5. Using global or static member pointers
    6. Allocating memory without storing the pointer, e. g.  func(new int[100]);

    Regrads, Alex
    LVL 10

    Expert Comment

    The definition of a memory leak is the loss of a dynamically allocated block of memory because all references to it are lost.

    Assigning the return value of malloc() to a local variable in a function, and then exiting the function without returning the contents of that variable is one common example of a memory leak.

    Also, setting another pointer via realloc(). to the original set via malloc() then later freeing the malloc'ed one is likely to cause problems too.

    strdup() is a standard C function that requires freeing - but perhaps you've got that covered as you mention free().

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now