使用 Memcheck 检测内存泄漏
使用Valgrind 工具套件中的 Memcheck 工具,可以检测应用程序中与内存管理相关的问题。将该工具与 GDB 调试器一起使用。检测到问题时,应用程序将被中断,你可以对其进行调试。
注意: 可以在 Linux 本地安装并运行 Memcheck。您可以从任何计算机在远程主机或设备上运行它。在 Windows 上,可以使用Heob堆观察器获得类似的结果。
下载并安装 Valgrind 工具后,可以从Qt Creator 使用 Memcheck。
检测应用程序中的内存泄漏:
- 进入Projects 模式,选择调试构建配置。
- 在模式选择器中,选择Debug >Memcheck 。
- 选择
启动应用程序。
- 使用应用程序进行分析。
- 选择
,在Memory Issues 中查看分析结果。
查看内存问题
当应用程序运行时,Memcheck 会执行以下操作:
- 检查所有内存读写。
- 拦截分配或释放内存或创建或删除内存块的调用。
停止 Memcheck 后,就能看到结果。
选择一行,查看发生内存泄漏的位置,以及显示造成泄漏原因的堆栈跟踪。
作为收集数据的替代方法,选择 可将 XML 格式的外部日志文件加载到Memcheck 视图中。
在某一行上移动鼠标,可查看有关函数的更多信息。
要在行间移动,请选择 或
。
要过滤结果,请选择 ,然后选择要在视图中显示的问题类型。您可以查看和隐藏确定的和可能的内存泄漏、未初始化的值、对
free()
的无效调用以及外部错误。
有关使用 Memcheck的更多信息,请参阅 Valgrind 文档中的解读 Memcheck 输出。
另请参阅 如何:分析、剖析函数执行、在外部应用程序上运行 Valgrind 工具、为项目指定 Valgrind 设置、分析器、Valgrind Callgrind、Valgrind Memcheck 和分析代码。
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.