Detect memory leaks with Memcheck tool provided by Valgrind - Part IValgrind is a popular instrumentation framework that can be used for
variety of tasks like memory memory error detecting, thread error
detecting, call graph generation etc. Though it can be used for multiple
tasks, it's the memory leak detection for which it is used the most. Here in this article, we will discuss how Valgrind can be used to detect memory leaks using a practical C example. More informationHere is more information on this tool (from official website):On the memory error detector tool the website says : The Valgrind tool suite provides a number of debugging and profiling tools that help you make your programs faster and more correct. The most popular of these tools is called Memcheck. It can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredictable behaviour. How memcheck worksAs already quoted above, the memcheck tool is used to detect memory leaks and other memory related problems in a C/C++ program. Through an example code, let's understand how memcheck works.Here is an example C code : #include<stdio.h> #include<string.h> #include<stdlib.h> int main(void) { char *ptr = (char*)malloc(10); memset(ptr, 0, 10); strncpy(ptr, "Linux", strlen("Linux")); printf("\n ptr = [%s]\n", ptr); free(ptr); ptr[0] = 'a'; return 0; } In the code above, if you analyse, you will see that the pointer 'ptr' is allocated 10 bytes of memory and is then populated with the string "Linux" which is printed on stdout using the printf() function. Then the memory pointed by 'ptr' is freed using the free() function. Even after this the code is trying to access the freed memory through ptr[0]. This is wrong but remains undetected. Let's use the memcheck tool provided by Valgrind to detect this problem. Here are some of the steps that you should follow :
Here is how I performed these steps with the example code above : $ valgrind valgrind: no program specified valgrind: Use --help for more information.This made sure that valgrind is installed in my system. $ gcc -Wall -g memcheck.c -o memcheck $This made sure that the code (memcheck.c)was compiled uaing gcc with debugging information ON. Finally, the memcheck tool was run through Valgrind and the following output was produced : $ valgrind --leak-check=yes ./memcheck ==3608== Memcheck, a memory error detector ==3608== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==3608== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==3608== Command: ./memcheck ==3608== ptr = [Linux] ==3608== Invalid write of size 1 ==3608== at 0x80484AD: main (memcheck.c:16) ==3608== Address 0x41f2028 is 0 bytes inside a block of size 10 free'd ==3608== at 0x402B06C: free (in /usrSo we see that the memcheck tool detected an Invalid write of size 1 byte at line 16 of memcheck.c file which was already freed at line 14 in the same file. So we see that memcheck tool was able to successfully detect the problem in the code. There are a variety of memory related issues that memcheck tool can detect. Here is one more example : #include<stdio.h> #include<string.h> #include<stdlib.h> int main(void) { char *ptr = (char*)malloc(10); memset(ptr, 0, 10); strncpy(ptr, "Linux", strlen("Linux")); printf("\n ptr = [%s]\n", ptr); ptr[0] = 'a'; printf("\n ptr = [%s]\n", ptr); return 0; }We see that in the code above, the memory allocated to the pointer 'ptr' was never freed in the program. Let's use memcheck to see if the tool can detect this problem : $ valgrind --leak-check=yes ./memcheck ==3632== Memcheck, a memory error detector ==3632== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==3632== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==3632== Command: ./memcheck ==3632== ptr = [Linux] ptr = [ainux] ==3632== ==3632== HEAP SUMMARY: ==3632== in use at exit: 10 bytes in 1 blocks ==3632== total heap usage: 1 allocs, 0 frees, 10 bytes allocated ==3632== ==3632== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==3632== at 0x402BE68: malloc (in /usrThe memcheck tool was able to detect the problem and reported it in the output (see lines in bold above). NOTE : If the memcheck tool starts giving some weird results related to line numbers etc then may be you can compile the code with optimizations turned off. For gcc, you can use the option -o0. Some Important Links |