Sunday, February 14, 2016

Check For Memory Leaks In Programs

Check For Memory Leaks In Programs


#include 
 
  void f(void)
  {
     int* x = malloc(10 * sizeof(int));
     x[10] = 0;        // problem 1: heap block overrun
  }                    // problem 2: memory leak -- x not freed
 
  int main(void)
  {
     f();
     return 0;
  }
You can compile and run it as follows to detect problems:
gcc test.c
valgrind --log-file=output.file --leak-check=yes --tool=memcheck ./a.out
vi output.file

How Do I use Valgrind?

If you normally run your program like this: ./a.out arg1 arg2 OR /path/to/myapp arg1 arg2 Use this command line to turn on the detailed memory leak detector: valgrind --leak-check=yes ./a.out arg1 arg2 valgrind --leak-check=yes /path/to/myapp arg1 arg2 You can also set logfile: valgrind --log-file=output.file --leak-check=yes --tool=memcheck ./a.out arg1 arg2 Most error messages look like the following: cat output.file

Detecting kernel memory leaks

http://lwn.net/Articles/187979/

No comments:

Post a Comment