# Valgrind Memcheck

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/clion/llms.txt).

> **TL;DR**
> 操作系统  ：Linux / WSL / macOS（10.12 或更高版本）
>
>
>
> 项目格式  ： [CMake](quick-cmake-tutorial.html) / [Makefile](makefiles-support.html) / [编译数据库](compilation-database.html)，带有 [自定义构建目标](custom-build-targets.html) / [Gradle](gradle-support.html)
>
>
>
> 工具链  ：local / [remote](remote-projects-support.html) / [WSL](how-to-use-wsl-development-environment-in-product.html) / [Docker](clion-toolchains-in-docker.html)

[Valgrind Memcheck](http://valgrind.org/docs/manual/mc-manual.html) 是一个工具，用于检测内存使用问题，例如泄漏、无效的内存访问、不正确的内存释放以及未定义的值引用。

在 CLion 中，Valgrind 集成可在 Linux、macOS 和 Windows 上使用 [WSL](how-to-use-wsl-development-environment-in-product.html)（请参阅 [Valgrind on WSL](#valgrind-wsl)）。

Procedure: 配置 Valgrind

1. 在您的系统上 [安装](http://valgrind.org/downloads/?src=www.discoversdk.com) Valgrind。

> **Note:**
> 如果您使用 WSL，请在您的 WSL 实例上安装 Valgrind（  `sudo apt-get install valgrind` ），并为 CLion 提供可执行文件的路径（请参阅步骤 3）。

2. 在 CLion 中，转到  `设置 | 构建、执行、部署 | 动态分析工具 | Valgrind` 。

3. CLion 将尝试检测系统中设置的 Valgrind 二进制文件  `PATH` 。

如果 Valgrind 位于非标准位置，请在   Valgrind 可执行文件   字段中手动提供路径。

4. 指定 [分析选项](http://valgrind.org/docs/manual/mc-manual.html#mc-manual.options)或使用默认选项。

5. 可选地，配置 [抑制文件](http://valgrind.org/docs/manual/manual-core.html#manual-core.suppress)列表。

例如，可以使用抑制文件关闭对特定库的 Valgrind 检查。

![Valgrind 设置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_valgrindSettings.png)

Procedure: 在 WSL 上配置 Valgrind

1. 在  `设置 | 构建、执行、部署 | 动态分析工具 | Valgrind`  中，提供 Valgrind 的“子系统”路径，默认为  `/usr/bin/valgrind` ，而不是 Valgrind 二进制文件的实际 Windows 位置。

2. 激活与 WSL 工具链连接的 [CMake 配置文件](cmake-profile.html)。

为此，请执行以下操作之一：

* 将 WSL 工具链设置为默认。 这样，它将自动连接到默认的 CMake 配置文件： ![WSL 工具链设置为默认](https://resources.jetbrains.com/help/img/idea/2026.3/cl_wsl_deftoolchain.png)

* 创建一个单独的 CMake 配置文件，将其连接到 WSL 工具链，并在配置切换器中选择此配置文件： ![WSL 工具链的 CMake 配置文件](https://resources.jetbrains.com/help/img/idea/2026.3/cl_wsl_cmakeprofile.png)

Procedure: 运行目标

> **Note:**
> 确保在包含调试信息的构建上运行 Valgrind。 否则，Valgrind 报告可能不完整或不准确。

要使用 Valgrind Memcheck 运行目标（应用程序或测试），请执行以下操作之一：

* 点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.execute.svg) 图标，并从菜单中选择   使用 Valgrind Memcheck 运行 'your_target_name'   选项：

![cl_runValgrindGutter.png](https://resources.jetbrains.com/help/img/idea/2026.3/调试](run-debug-configuration.html)配置，并从主菜单调用  `运行 | 使用 Valgrind Memcheck 运行 'your_target_name'`  或点击专用工具栏图标：

![从主工具栏运行 Valgrind](https://resources.jetbrains.com/help/img/idea/2026.3/cl_Valgrind_Run_icon.png)

如果之前未配置 Valgrind，CLion 将打开配置对话框。

> **Tip:**
> 在使用 [Google sanitizers](google-sanitizers.html)的项目上运行时，Valgrind 可能无法正常工作。 CLion 将向您发出警告：
>
> ![关于 Valgrind 与 Google Sanitizers 的警告](https://resources.jetbrains.com/help/img/idea/2026.3/cl_valgrind_sanitizers_warning.png)

Procedure: 查看结果

* 启动完成后，在   Valgrind   选项卡中打开   运行   工具窗口。

![Valgrind Memcheck 输出](https://resources.jetbrains.com/help/img/idea/2026.3/cl_valgrind_outputnew.png)

* 左侧窗格显示 Valgrind Memcheck 找到的所有问题的列表。 每个条目包括指令指针、问题函数名称，以及（如果可能）问题所在的源文件。

* 在右侧窗格中，您可以找到完整的堆栈帧描述和相应的代码预览（如果可能）。

> **Tip:**
> 按 `F4` (Windows), `⌘ ↓` (macOS), `F4` (IntelliJ IDEA Classic (macOS)), `F4` (macOS System Shortcuts), `F4` (XWin), `F4` (GNOME), `F4` (KDE), `F4` (Emacs), `F4` (Sublime Text), `F4` (Sublime Text (macOS)), `⌘ ↓` (Xcode), `F4` (Visual Studio), `⌘ ↓` (Visual Studio (macOS)), `F4` (ReSharper), `⌘ ↓` (ReSharper (macOS)), `F4` (QtCreator), `F4` (QtCreator (macOS)), `F4` (NetBeans), `F12` (Eclipse), `F3` (Eclipse (macOS)) 返回源代码。
>
>
>
> 使用上下文菜单操作将警告数据复制到剪贴板：
>
>
>
> ![Valgrind 视图上下文操作](https://resources.jetbrains.com/help/img/idea/2026.3/cl_valgrind_view_actions.png)

Procedure: 导出和导入结果

* 要导出结果，请点击   导出   ![cl_valgrindexport_b.png](https://resources.jetbrains.com/help/img/idea/2026.3/cl_valgrindexport_b.png) 图标，位于   Valgrind   选项卡的左侧窗格中，并指定目标 XML 文件：

![导出 Valgrind 结果](https://resources.jetbrains.com/help/img/idea/2026.3/cl_valgrind_export.png)   注释   导出按钮在执行期间不可用。

* 要导入结果，请从主菜单中选择  `运行 | 导入 Valgrind XML 结果` ，并选择要导入的 XML 文件。

