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.
Install Valgrind on your system. After the successful installation, CLion automatically detects the Valgrind binary by searching in standard locations defined in the system
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 Valgrind executable field.and specify the path in the
In the same dialog, specify the required analysis options.
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.
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:
Create a separate CMake profile, connect it to the WSL toolchain, and select this profile in the configurations switcher:
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, chooseoption.
Click the dedicated icon in the Navigation bar:
Click the gutter icon and select Run 'your_target_name' with Valgrind Memcheck option from the menu:
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:
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 icon in the left pane of the Valgrind tab and specify the destination XML file: Note that the Export button is unavailable during execution.
To import the results, selectfrom the main menu and choose the XML file to import from.