basysKom Toolbox

15 – Valgrind or the MemorySanitizer hel to ship reliable software

Valgrind or the MemorySanitizer help to ship reliable software

Goals

Do not rely on uninitialized memory.

Description

C and C++ don’t force you to initialize variables. Uninitialized variables introduce lingering bugs and hard to find random behavior. While modern compilers can warn for some of these issues, Valgrind and/or the MemorySanitizer can find additional ones. Note that both tools are, unlike the compiler, dynamic and rely on code execution and good code coverage. Consider to combine them with unit tests automatically run in a CI system for extra power.

Environment

C and C++/Qt

Platform

As a rule of thumb – Valgrind is available for most Unix-like platforms. See https://valgrind.org/info/platforms.html for details.

The MemorySanitizer is currently only available with clang.

Implementation effort

If a program is already runnable on X86_64/Linux Valgrind can be employed very easily.
While quite powerful – the MemorySanitizer is much harder to deploy. Unlike for example the AddressSanitizer it can’t be partially applied to a program.
Rule of thumb: try to use Valgrind first.

Applicability

Caveats

  • While Valgrind is available for a lot of Unix-like platforms, but practical usage is typically limited by the raw CPU speed of the target. Use on ARM is often not practical.

See also

Implementation hints

  • Valgrind can produce false positives especially for third party and system libraries. Valgrind supports to concept of a “suppression file” to selectively mute these. Put your project specific suppression files into version control with the rest of your project.
  • Valgrind is also reporting on memory leaks and issues such as various forms of memory corruption.
About the Toolbox

The basysKom Toolbox is a state-of-the-art collection of best practices in agile management and software development and a valuable tool for every kickoff meeting.

These cards can be used during a project kickoff in order to select best fitting good practices from the very start of your project, but also as a reminder during the course of a project implementation. Every card holds a QR code with additional information related to the specific tool, the tools’ applicability, known constraints and additional tips on how to implement it in a project setup.

You can provide us feedback on GitHub.

Toolbox

basysKom Newsletter

We collect only the data you enter in this form (no IP address or information that can be derived from it). The collected data is only used in order to send you our regular newsletters, from which you can unsubscribe at any point using the link at the bottom of each newsletter. We will retain this information until you ask us to delete it permanently. For more information about our privacy policy, read Privacy Policy