Go back to blog listing

Finding Memory Leaks in C or C++

A literal leak

Unlike the picture above, memory leaks in software can be hard to pinpoint. Learn how to find memory leaks in C and C++ applications with the help of a runtime error detection tool. 

What is a memory leak?

Those writing code in C or C++ will be familiar with memory leaks. Leaks mean that dynamically-allocated memory cannot be released back to the operating system because the program no longer contains pointers that can access it. In other words, you've lost control of that piece of memory and can no longer access it or free it. 

A simple example of this behavior can be seen by running the “Hello world” program (the code is listed in our previous blog about how to find pointer abuse in C) with the arguments:

   hello this is a test

If we examine the state of the program at line 25, just before executing the call to malloc for the second time, we observe:

  • The variable string_so_far points to the string “hello” which it was assigned as a result of the previous loop iteration.
  • The variable string points to the extended string “hello this” which was assigned on this loop iteration.

These assignments are shown schematically below; both variables point to blocks of dynamically allocated memory.

The next statement:

string_so_far = string;

will make both variables point to the longer memory block as shown below:

Both variables point to longer memory block

Once this happens, however, there is no remaining pointer that points to the shorter block. Even if you wanted to, there is no way that the memory that was previously pointed to by string_so_far can be reclaimed; it is now permanently allocated. This is known as a “memory leak.”

How do you find and fix memory leaks?

This type of error can be diagnosed by memory error detection tools, such as Parasoft Insure++. This is shown below:

[hello.c:25] **LEAK_ASSIGN**
>>         string_so_far = string;
  Memory leaked due to pointer reassignment: string
  Lost block : 0x0804bd68 thru 0x0804bd6f (8 bytes)
               string, allocated at hello.c, 15
                          malloc()  (interface)
                            main()  hello.c, 15
  Stack trace where the error occurred:
                            main()  hello.c, 25

This example is called LEAK_ASSIGN because it is caused when a pointer is re-assigned. (P.S. Other memory debuggers often don't make a distinction between outstanding memory and actual leaked memory, but Insure++ does.) In this case, outstanding memory isn't memory that's awesome, it's memory that you didn't free, as distinct from an actual leak which is memory that you cannot free.

Types of memory leaks

Parasoft Insure++ can also detect several other types of leaks automatically:

Leak type

Description

LEAK_FREE

Occurs when you free a block of memory that contains pointers to other memory blocks.

LEAK_RETURN

Occurs when a function returns a pointer to an allocated block of memory, but the returned value is ignored in the calling routine.

LEAK_SCOPE

Occurs when a function contains a local variable that points to a block of memory, but the function returns without saving the pointer in a global variable or passing it back to its caller.

Notice that the error message indicates the exact source line on which the problem occurs, not just where the block was allocated, which is a key issue in finding and fixing memory leaks. This is extremely important because it’s easy to introduce subtle memory leaks into your applications, but very hard to find them all.

To start cleaning memory leaks from your code, you can get a free trial of Parasoft Insure++.

Get the ultimate memory debugger for C and C++

Stay up to date