Valgrind Quick Reference Guide: How to Use Valgrind to Detect C++ Memory Leaks

Valgrind Quick Reference Guide: How to Use Valgrind to Detect C++ Memory Leaks

Valgrind MemCheck and Valgrind Helgrind

Most C and C++ programmers have heard of Valgrind, a framework that can instrument a program for detecting C and C++ memory leaks during execution (or memory management errors that can lead to crashes or unpredictable behavior).

Valgrind can run several different tools for detecting different kinds of errors or visualizing other information about the program’s execution. The most common tool is Valgrind MemCheck, which is what most programmers think about when someone says “Valgrind”. MemCheck watches all uses of malloc, free, and similar library functions, and detects and summarizes many different kinds of C++ memory leaks in the program. It also detects a lot of other memory management errors like using memory after it’s been freed, which can cause a crash one day even if it runs successfully now.

But Valgrind offers more than MemCheck. One of the tools which is incredibly useful is Helgrind, which can detect race conditions and a lot of other common synchronization errors in threaded programming, in any program using POSIX pthreads.

I know that a lot of experienced programmers have tried Valgrind tools, but don’t really know which parts of the MemCheck output are problems they ought to fix and which are normal things they can ignore.

 

How to Use Valgrind for C++ Memory Leak and Race Condition Detection

I recently wrote a Quick Reference Guide for MemCheck and Helgrind, showing the most common options to use, the most common problems getting started, and the most common errors that are reported and what they actually mean. I learnt a lot in the process!

The Quick Reference Guide is available as a  PDF so you can easily print it out if you want to.

How to use Valgrind

Click on the image to access the quick reference guide.

 

Author: Jack Vickeridge, Senior Software Engineer at Undo

New call-to-action

Stay informed. Get the latest in your inbox.