首页 > 代码库 > 【Valgrind】How to check memory leak and where it's in 10 mins

【Valgrind】How to check memory leak and where it's in 10 mins

1. Install

sudo apt-get install valgrind

2. If memory leak

example code:

/* memleak.c */#include <stdlib.h>void* memleak(int n){    void *p = malloc(n);    return p;}
memleak.c
/* main.c */#include <stdio.h>#include <stdlib.h>void* memleak(int n);int main(){     int i=0;     void *p = NULL;     for(i=0; i<10; i++)     {     p = memleak(100);         printf("allocate memory address: %p\n", p);     }     return 0;}
main.c

Use ‘valgrind ./exe_binary [cmdline]‘ to check if memory leak happens:

josh@josh-VirtualBox:~/test_memleak$ gcc main.c memleak.c -O1 -o main.xjosh@josh-VirtualBox:~/test_memleak$ valgrind ./main.x==6726== Memcheck, a memory error detector==6726== Copyright (C) 2002-2011, and GNU GPLd, by Julian Seward et al.==6726== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info==6726== Command: ./main.x==6726==allocate memory address: 0x51f2040allocate memory address: 0x51f20f0allocate memory address: 0x51f21a0allocate memory address: 0x51f2250allocate memory address: 0x51f2300allocate memory address: 0x51f23b0allocate memory address: 0x51f2460allocate memory address: 0x51f2510allocate memory address: 0x51f25c0allocate memory address: 0x51f2670==6726====6726== HEAP SUMMARY:==6726==     in use at exit: 1,000 bytes in 10 blocks==6726==   total heap usage: 10 allocs, 0 frees, 1,000 bytes allocated==6726====6726== LEAK SUMMARY:==6726==    definitely lost: 1,000 bytes in 10 blocks==6726==    indirectly lost: 0 bytes in 0 blocks==6726==      possibly lost: 0 bytes in 0 blocks==6726==    still reachable: 0 bytes in 0 blocks==6726==         suppressed: 0 bytes in 0 blocks==6726== Rerun with --leak-check=full to see details of leaked memory==6726====6726== For counts of detected and suppressed errors, rerun with: -v==6726== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
 

3. Where memory leak

$ gcc main.c memleak.c -O1 -g -o main.x 

$ valgrind --leak-check=full ./main.x

ATTENTION:

  • "-O1" to prevent from inline functions by optimization, "-g" to get line number of memory leak functions.
  • In one word, use "DEBUG" flavour instead of "release" while finding memleak.
josh@josh-VirtualBox:~/test_memleak$ valgrind --leak-check=full ./main.xjosh@josh-VirtualBox:~/test_memleak$ valgrind --leak-check=full ./x==7209== Memcheck, a memory error detector==7209== Copyright (C) 2002-2011, and GNU GPLd, by Julian Seward et al.==7209== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info==7209== Command: ./x==7209==allocate memory address: 0x51f2040allocate memory address: 0x51f20f0allocate memory address: 0x51f21a0allocate memory address: 0x51f2250allocate memory address: 0x51f2300allocate memory address: 0x51f23b0allocate memory address: 0x51f2460allocate memory address: 0x51f2510allocate memory address: 0x51f25c0allocate memory address: 0x51f2670==7209====7209== HEAP SUMMARY:==7209==     in use at exit: 1,000 bytes in 10 blocks==7209==   total heap usage: 10 allocs, 0 frees, 1,000 bytes allocated==7209====7209== 1,000 bytes in 10 blocks are definitely lost in loss record 1 of 1==7209==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)==7209==    by 0x4005A3: memleak (memleak.c:5)==7209==    by 0x400573: main (main.c:12)==7209====7209== LEAK SUMMARY:==7209==    definitely lost: 1,000 bytes in 10 blocks==7209==    indirectly lost: 0 bytes in 0 blocks==7209==      possibly lost: 0 bytes in 0 blocks==7209==    still reachable: 0 bytes in 0 blocks==7209==         suppressed: 0 bytes in 0 blocks==7209====7209== For counts of detected and suppressed errors, rerun with: -v==7209== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)josh@josh-VirtualBox:~/test_memleak$
 

You can see the call stack, which means where memory leak happens

main() ==> memleak() ==> malloc()

4. Find memory leak because of invoking 3rd-party library

Use the same method as above.