7 Things You Need to Know about Memory Leaks in C
Garbage collection is implemented in the majority of high-level programming languages. In C programming language, there are no signs of automatic garbage collection. Thus, programmers of this low-level language are responsible for the memory management that occurs within a program. Memory leaks are, in a way, a consequence of poor memory management.
The seven points listed below will give you a deeper insight into memory leaks and ways to deal with them.
1. What is a memory leak?
A memory leak is a block of dynamically allocated memory that is not freed and that has lost all the references to it. In C programming language this occurs, when heap
memory is dynamically allocated with functions, like malloc
or calloc
but not freed (with the function free
).
2. Do memory leaks occur on the stack in C?
No. Memory leaks do not occur on the stack in C because stack objects have automatic storage duration. Whenever an object leaves its scope, it is automatically freed.
Memory leaks only occur because of the improper handling of those objects that have dynamic storage duration. In other words, memory leaks can only occur with heap objects.
3. What types of programming mistakes lead to memory leaks?
There are three common mistakes that lead to memory leaks.
(1) Improper handing of a return value (consider line 10):
(2) Improper freeing of an array of pointers (consider lines 7 and 8):
(3) Improper reassignment of a pointer pointing to dynamically allocated memory block (consider line 6 and 7):
4. How to prevent memory leaks in C?
For one, it is a great idea to avoid the most common memory management mistakes discussed in the answer above. For two, it helps to follow the “Golden Rule”, which states that for each and every malloc and calloc call, you should call a free function.
This applies to every path of execution. This means that if an error occurs, dynamically allocated memory should still be freed. Just like it is freed in the example below (consider the line 13).
5. In what types of applications are memory leaks especially dangerous?
Memory leaks are never desirable, to say the least. However, they are way more dangerous and detrimental to long-running application. Such applications, among others, include (1) servers, (2) browsers and (3) graphic editors.
In these and other long-running applications memory leaks have a higher probability to result in crashes, given that the number of leaks continuously grow over time.
6. How to find memory leaks in C?
In order to find memory leaks, you may reserve to, at least, two options. One is to use runtime detection tools like Valgrind or Dr. Memory. Two, implement a pair of counters of allocated and deallocated memory or of malloc
and free
function calls.
Using the counter functions helps you to keep track of all the allocations and deallocations within your program. The Is_all_memory_freed
function informs you, if all the allocated memory is freed. So, calling this function before every exit from your program would inform you if the memory in your program is managed in a proper way.
7. Do memory leaks pose security risks?
Yes. Memory leaks pose security risks to publicly accessible applications. If a malicious user can cause a memory leak by a certain sequence of actions in a publicly accessible application (think of a web-browser or a router), then such an application can be strongly affected by the malicious users.
Summary
To sum it all up, memory leaks are lost references to memory addresses that are not freed. These lost references, in turn, downgrade your software and make it more prone to crashes and vulnerable to malicious attacks.
To improve the quality of your programs, prevent memory leaks by following the best practices and utilize a tracking option for memory leaks. So that your application run memory leaks free.