CLion 2020.1 Help

Valgrind Memcheck

Valgrind Memcheck is a tool that helps identify memory errors in C and C++ code. It can detect various problems such as memory leaks, invalid memory access, incorrect freeing of memory, and using undefined values.

Valgrind Memcheck in CLion works on multiple platforms: Linux, macOS, and Windows via WSL (see Valgrind on WSL).

Configure Valgrind

  1. Install Valgrind on your system. After the successful installation, CLion automatically detects the Valgrind binary by searching in standard locations defined in the system PATH variable.

    For WSL, you need to install Valgrind on your WSL instance (sudo apt-get install valgrind) and provide CLion with the path to the executable (see the next step).

    In case of non-standard location of Valgrind, you can point CLion to it explicitly: go to Settings / Preferences | Build, Execution, Deployment | Dynamic Analysis Tools | Valgrind and specify the path in the Valgrind executable field.

  2. In the same dialog, specify the required analysis options.

  3. Optionally, configure the list of the suppression files. For example, you may want to add a non-default suppression file to turn off the Valgrind Memcheck checks for some particular libraries. The typical case here, for example, would be to suppress OpenSSL or Boost.

Valgrind settings

Configure Valgrind on WSL

To use Valgrind on Windows via WSL, provide the 'subsystem' path to Valgrind, which is /usr/bin/valgrind by default, instead of the actual Windows location of the Valgrind binary. However, this path will not be valid until you select the CMake profile connected to the WSL toolchain. For this, do one of the following:

  • Set the WSL toolchain as default. This way, it will automatically connect to the default CMake profile:

    cl wsl deftoolchain png

  • Create a separate CMake profile, connect it to the WSL toolchain, and select this profile in the configurations switcher:

    cmake profile for wsl toolchain

Run targets

To run a target (application or test) with Valgrind Memcheck, select the desired Run/Debug configuration, and then do one of the following:

  • On the main menu, choose Run | Run 'your_target_name' with Valgrind Memcheck option.

  • Click the dedicated icon in the Navigation bar:

    cl Valgrind Run icon png

  • Click the icons.actions.execute.png gutter icon and select Run 'your_target_name' with Valgrind Memcheck option from the menu:

    cl runValgrindGutter png

Explore results

  • The left-hand pane of the Valgrind tab in the Run tool window represents information of all problems found by Valgrind Memcheck. Each stack-frame shows the instruction pointer, problematic function name, and (if possible) the source file where the problem is located:

    Valgrind Memcheck output

  • Select a stack-trace of interest. In the right-hand pane, you can view the full stack frame description of a target stack-trace and the corresponding code preview (when possible). Press F4 to jump back to the source code.

  • Right-click the selected item to exclude it from the current results log or suppress the problematic function by adding a corresponding record to the suppression file.

Export and import results

  • To export the results, click the Export cl valgrindexport b png icon in the left pane of the Valgrind tab and specify the destination XML file:

    Export Valgrind results
    Note that the Export button is unavailable during execution.

  • To import the results, select Run | Import Valgrind XML Results from the main menu and choose the XML file to import from.

Last modified: 15 July 2020