One of the reasons I love using Eclipse is its ability to support various external tools through plugins. One such I used quite a bit and wanted to use it with Eclipse was Valgrind.
Valgrind found here, is a profiling tool which can be used to profile a program, mainly to detect memory leaks and bugs in threads if any used. Valgrind has a front end application called Valkyrie.
I prefer using Eclipse for most of the things I do while I code because I like to keep things in one place. So I started looking around in the web for a Valgrind plugin for Eclipse. Once I found it, it didn’t take me any time at all to set it up and profile my application right from Eclipse and fix issues easily.
The following steps describe the installation and usage. (I have tested this with Eclipse Indigo running on a Redhat machine. It should be similar on other Linux distributions)
- First make sure that Valgrind is installed on your machine. Get the same from here.
- Install the Valgrind plugin for Eclipse. To do this:
- Launch Eclipse, Click on Help -> Install New Software
- Select the software source for the version of Eclipse running. Once the software list is updated, in the filter box type Valgrind.
- From the results shown below, select the plugin for Valgrind and proceed to complete the installation. Once the installation is complete, Eclipse will restart.
- To verify if the plugin is installed correctly, select Windows -> Preferences -> Valgrind. In the resulting screen, enter the location of the Valgrind program installed in the first step. Default would be `
- Now load up your program in Eclipse. I will be using a simple C program here. In the file below, I haven’t freed the memory I allocated for the string.
- Build the project for Debug configuration (or gcc with -g option). This helps Valgrind to point out the source file and the line number where there’s a memory leak.
- Now select Run -> Profile Configuration. From the resulting dialog, select Profile with Valgrind. This will create a new configuration shown in the second picture below.
- In the ‘Valgrind Options’ tab shown in the picture above, adjust any Valgrind specific settings (viz., the tool to be used for checking memory leaks and many other options. Refer to Valgrind’s man pages for information on them.) Once this is complete, click on ‘Profile’ to begin the memory leak check.
- Once the check is complete, a window is shown with a list of possible leaks. As shown in the picture below, Valgrind has pointed out the exact location in the file where the memory was leaked. Thus it’s easy to find out where the leaks are and fix them right away in the editor.
For complex projects, using Valgrind in the command line becomes very tedious. With this integration (or with Valkyrie for that matter), the job is much easier and time saving.
Hope this helps.