Saturday, February 23, 2013




Memory Debugging
A Crash course

To get rid of stack errors, buffers, exceptions or segmentation faults , leaked memories, pointer bounds, etc while using pointers and files, and string operations is not straightforward. These bugs are inevitable while programming and can cause weird behaviour on different machines, etc. Most of the time these errors or bugs can be removed cleanly using simple debugging tools. I have been using "valgrind" which is simple, intuitive, and a powerful tool to debug memory / data / file operations generated errors. These are some simple steps that I have used and found greatly beneficial while debugging my code ( a numerical solver for parallel architectures).

1. Debug your code with possible checks wherever needed (or every where) using "assert". It is a good practice to insert assert lines after pointer allocation, file opening, possible constraints on the input data (like x > 0, when read from a file), etc

2. Compile your code with a debug flag "-g" to include the debug symbols required.

3. Just run your code with "valgrind" as follows. And valgrind suggest the possible errors and other flags which will help it to determine help you to locate the source of the error.
  • valgrind ./a.out   [ simple run, and valgrind will further suggest you for possible flags required to run ]
  • valgrind -v --leak-check=full --track-origins=yes  ./a.out [ full check (leak check) and locate source of the problem (track origin) and output on the screen (-v) ]
This will help you start up the and get into proper memory debugging. Sort out more help on valgrind's website.

Also, valgrind can be used for any parallel application (like using MPI / OpenMP) as well.


0 comments:

Subscribe to RSS Feed Follow me on Twitter!