How to locate a memory leak in C?

Though it is an extremely programming intensive task to fix a memory leak, still if possible to get any help in finding the leaking source hint, it is really very helpful, trust me.

On Ubuntu you may get many memory leak fix solutions. But the best which did saved the day turned up to be Valgrind... tada...

If you don't have valgrind installed you can install it by
apt-get install valgrind

Valgrind comes with a command prompt interface. Although its GUI clients are also available like Valkyrie etc, but command line interface is enough.

To check memory leak location you can execute the following command definitely with all the supportive arguments

valgrind --leak-check=full ./executable arg1 arg2


This will show the memory leak in the bottom up fashion showing the stack. For instance if there is a memory leak in func1(), than the stack would show something like:

malloc()
func1()
main()

You have to intutively identify the hint of the stack. For example the above stack shows that there is some allocation using malloc() within the func1() which is not de-allocated properly. Hence this means that a call to free() is missing. main() is here as always a starting location.

So, if you want to explore more about it, you can check http://www.faqs.org/docs/Linux-HOWTO/Valgrind-HOWTO.html, which is really a good resource to begin with.

Enjoy fixing leaks in the memory.

Comments

Popular posts from this blog

Imote2 with Camera setup IMB400 over Ubuntu 9.10/10.04

Branch and bound algorithm in C#- Article by AsiF Munir

Tensorflow - Simplest Gradient Descent using GradientDescentOptimizer