c++ - 修复 Valgrind 错误内存泄漏的错误

标签 c++ ubuntu memory-leaks valgrind

我昨天在我的 Windows 计算机上安装了 Ubuntu(终端)。此外,当提前安装 Valgrind 时,“sudo apt install Valgrind”,为了测试所有内容,我继续创建了一个 c++ hello world 程序。然后用 Valgrind 测试它。 Valgrind 说我有大约“1 个 block 中有 72,704 个字节”。进一步的研究表明,这是 Valgrind 与 c++ 标准库函数(可能是 iostream)的错误。我的问题是我该如何着手修复这个错误。我不能忽视,因为如果我正在处理程序,我需要能够准确地判断它的来源。如果有人可以用外行人的术语为我的问题提供逐步解决方案,那将是无价的。 这是我的代码和我得到的错误:

#include <iostream>
using std::cout; using std::endl;

int main() {
        cout << "Hello World" << endl;
}

==195== Memcheck, a memory error detector
==195== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==195== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==195== Command: ./helloworld
==195==
Hello World
==195==
==195== HEAP SUMMARY:
==195==     in use at exit: 72,704 bytes in 1 blocks
==195==   total heap usage: 2 allocs, 1 frees, 76,800 bytes allocated
==195==
==195== LEAK SUMMARY:
==195==    definitely lost: 0 bytes in 0 blocks
==195==    indirectly lost: 0 bytes in 0 blocks
==195==      possibly lost: 0 bytes in 0 blocks
==195==    still reachable: 72,704 bytes in 1 blocks
==195==         suppressed: 0 bytes in 0 blocks
==195== Rerun with --leak-check=full to see details of leaked memory
==195==
==195== For counts of detected and suppressed errors, rerun with: -v
==195== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

更新: 似乎我可以通过创建抑制文件来对误报泄漏进行分类处理。但是,与这里的某些人相比,我并不精通技术,不知道如何做到这一点。我将如何在 Valgrind 上创建抑制文件(专门针对我的情况)?请用 LAYMAN 的术语进行解释,并尽可能详细。 第二次更新: 我在抑制内存泄漏方面取得了部分成功,但我想知道是否有更可行的长期解决方案。

最佳答案

我会说这些很可能是真正的问题,但对于 libc/libstdc++ 开发人员来说可能太小而无法修复。

您可以通过指定 --gen-suppressions=yes 在 Valgrind 输出中生成抑制。这将生成如下输出:

==28328== 56 bytes in 1 blocks are still reachable in loss record 1 of 7
==28328==    at 0x4C290F1: malloc (vg_replace_malloc.c:298)
==28328==    by 0x4111D8: xmalloc (xmalloc.c:43)
==28328==    by 0x41120B: xmemdup (xmalloc.c:115)
==28328==    by 0x40F899: clone_quoting_options (quotearg.c:102)
==28328==    by 0x40742A: decode_switches (ls.c:1957)
==28328==    by 0x40742A: main (ls.c:1280)
==28328== 
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc
   fun:xmalloc
   fun:xmemdup
   fun:clone_quoting_options
   fun:decode_switches
   fun:main
}

在上面,带有 pid (==28328==) 的部分是通常的输出。它后面的部分(由大括号分隔)是生成的抑制。您可以将此 block 复制到一个文件中,例如 my_suppressions,然后您可以运行 valgrind 并告诉它读取文件 --suppressions=my_suppressions

如果您打算长期维护您的抑制文件,那么最好在“insert_a_suppression_name_here”的位置放置一些有意义(且唯一)的文本。这将帮助您监控正在使用哪些抑制,因为如果您以详细模式(-v 或 --verbose)运行 valgrind,它将列出所有使用的抑制。例如

--30822-- used_suppression:      2 Example for SO my_suppressions:2 suppressed: 112 bytes in 2 blocks
--30822-- used_suppression:      4 U1004-ARM-_dl_relocate_object /remote/us01home48/pfloyd/tools/vg313/lib/valgrind/default.supp:1431

关于c++ - 修复 Valgrind 错误内存泄漏的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45258930/

相关文章:

C++ MSVS 项目内链接器错误 LNK2019 - 声明定义不匹配?

python - Python中的OpenCV GUI问题

java 内存不足错误 : bitmap size exceeds VM budget

c - 我的 C 程序中存在内存泄漏

c++ - 在辅助显示器上工作

c++ - Windows Metro 模式 (C++) : Waiting on semaphore (WaitForSingleObjectEx): failed with Access Denied [partially solved]

c++ - int32_t 的延迟是否低于 int8_t、int16_t 和 int64_t?

laravel - "Could not find driver"执行查询时的消息

windows - git install LAN 在 ubuntu 服务器和 windows LAN 上

perl - 使用 HTTPS 的 Perl LWP 的内存泄漏