How to find memory leaks in C or C++?

Memory leaks in programming software can be difficult to pinpoint because of the large amount of data involved. In this article, you can learn how to find memory leaks in C and C++ applications with the help of runtime error detection tools.

What is a memory leak? C and C++ language examples

C++ and C have a set of runtime detection tools that can help improve performance when you face a memory leak. For those of you who frequently code in C or C++, memory leaks are no longer a surprise. The definition of memory leak in Wikipedia is as follows:

In computer science, a memory leak is a resource leak that occurs when a computer program mismanages memory allocation so that memory that is no longer needed cannot be freed. Memory leaks can also occur when an object is stored in memory but is inaccessible to running code.

The program shown below running "Hello world" is one of the best examples of this behavior.

picture

If we execute this program with the following parameters:

picture

If we check the status of the program on line 25, just before the second call to malloc is executed, we observe:

  • So far, the variable string points to the string "hello", which is the result of the previous loop iteration.

  • The variable string points to the extended string "hello-this" assigned during this loop iteration.

These assignments are diagrammed below; both variables point to dynamically allocated memory blocks.

Next sentence:

picture

Two variables pointing to longer blocks of memory will be created as follows:

picture

However, once this happens, there are no pointers left pointing to shorter blocks. Even if you wanted to, it would be impossible to reclaim the memory previously pointed to by string_so_far; it is now permanently allocated. This is called a "memory leak". C++ and C often face these common problems, so it's important to catch them early.

How to spot memory leaks in C++ and C?

Although there is no "Detect Memory Leak" button, there are runtime detection tools for C++ and C that can help. This type of error can be diagnosed with a memory error detection tool such as Parasoft Insure++ . As follows:

picture

This example is called LEAK_SSIGN because it is caused when the pointer is reallocated. (P.S. Other memory debuggers usually don't differentiate between unhandled memory and actual leaked memory, but Insure++ does.) In this case, unhandled memory isn't good memory, it's memory you didn't free, which is different from actual A leak is different, an actual leak is memory that you cannot free.

Types of memory leaks

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

Leak type describe
LEAK_FREE Occurs when a memory block containing a pointer to another memory block is freed.
LEAK_RETURN Occurs when a function returns a pointer to an allocated memory block, 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 the caller.

Note that the error message points to the exact source line where the problem occurred, not just where the block was allocated, which is key to finding and fixing memory leaks. This is important because it's easy to introduce subtle memory leaks in your application, but hard to find them all.

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/132758446