As your code gets bigger, keeping track of memory becomes critical. These things often become the source of huge crashes and annoying bugs. Detecting them manually is a very tedious process. So we need a tool to manage all this for us. This is where Valgrind comes into picture. Valgrind is a programming tool for memory debugging, memory leak detection, and profiling. It can be used to keep track of all the memory allocated at all stages in your program. Although it was initially meant to be a memory debugger, it has grown to become a generic framework for building dynamic analysis tools. You can use Valgrind to build new tools as well. There are Valgrind tools like Memcheck that can automatically detect many memory management and threading bugs, and profile your programs in detail. Valgrind is widely used in the industry and it’s well respected within the coding community. Let’s see how we can get it up and running on Mac OS X.
Assuming you have svn installed (usually comes pre-installed with OS X), get the latest valgrind repo by executing the following command:
>> svn co svn://svn.valgrind.org/valgrind/trunk valgrind
This will create a directory called “valgrind”. Make sure you have Macports installed. If not, install from here. Once you install it, use it to install “automake”:
>> sudo port install automake
For OS X Mountain Lion, running the above command might sometimes throw an error saying that the Xcode you have is not the latest version (even though you have the newer version). It will look something like this:
Error: The installed version of Xcode (2.0 or lower) is too old to use on the installed OS version. Version 4.5.2 or later is recommended on Mac OS X 10.8.
This happens because it uses some default path. Use the following command to change it:
>> sudo xcode-select -switch /Applications/Xcode46-DP3.app/Contents/Developer
Change the name from “Xcode46-DP3” to whatever version you have. Xcode46-DP3 refers to Xcode 4.6 Developer Version 3.
Now run the automake install command again and it will work.
We have to install another dependency called “autoconf” before proceeding further. Execute the following command to do it:
>> sudo port install autoconf
Now we are all set to install valgrind. Run the following commands to install it:
>> cd valgrind >> ./autogen.sh >> ./configure >> make >> sudo make install
And you are done! We are all set to use valgrind now. Execute the following command to use valgrind on your code.
>> g++ test.cpp >> valgrind ./a.out
This will display minimal information on the command line. To see full details, execute the following command:
>> valgrind --tool=memcheck --leak-check=yes --leak-check=full ./a.out arg1 arg2
Here, “a.out” is the default executable generated after compiling the code. If you using a different name, replace it appropriately.
A quick note before ending this post. Valgrind is supposed to be buggy for OS X Mountain Lion. The above installation procedure will work just fine but Valgrind may not detect memory leaks in a few cases. So just be careful about that!