Valgrind - 块仍然可以到达?

时间:2013-04-19 19:07:57

标签: valgrind memory-leaks

我想弄清楚是什么导致Valgrind警告某些“仍然可以到达”的区块。我查看了我的代码,找不到任何free()缺失。我尝试使用--leak-check = yes --show-reachable = yes选项运行,这是输出的示例:

==2999== 616 bytes in 7 blocks are still reachable in loss record 8 of 9
==2999==    at 0xD9C3: calloc (vg_replace_malloc.c:597)
==2999==    by 0x3188FA: _xpc_calloc (in /usr/lib/system/libxpc.dylib)
==2999==    by 0x3191D4: _xpc_base_create (in /usr/lib/system/libxpc.dylib)
==2999==    by 0x31FBEA: xpc_string_create (in /usr/lib/system/libxpc.dylib)
==2999==    by 0x31EDFD: xpc_dictionary_set_string (in /usr/lib/system/libxpc.dylib)
==2999==    by 0x320E8C: _libxpc_initializer (in /usr/lib/system/libxpc.dylib)
==2999==    by 0x1BE7D: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==2999==    by 0x7FFF5FC0FDA5: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==2999==    by 0x7FFF5FC0FAF1: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==2999==    by 0x7FFF5FC0D2E3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==2999==    by 0x7FFF5FC0D27C: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==2999==    by 0x7FFF5FC0E0B6: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)


==2999== 4,096 bytes in 1 blocks are still reachable in loss record 9 of 9
==2999==    at 0xC713: malloc (vg_replace_malloc.c:274)
==2999==    by 0x17F3F7: __smakebuf (in /usr/lib/system/libsystem_c.dylib)
==2999==    by 0x175D19: __swsetup (in /usr/lib/system/libsystem_c.dylib)
==2999==    by 0x1766C3: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==2999==    by 0x17618D: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==2999==    by 0x17F2CF: printf (in /usr/lib/system/libsystem_c.dylib)
==2999==    by 0x100003D21: printEstBasica (mod_dados.c:100)
==2999==    by 0x100001635: main (mod_main.c:179)

我理解的线条或者指的是'打印'功能(比如printEstBasica),我只使用printf或fprintf ......其他的我无法弄清楚它们是什么..我在其他帖子上看到人们说它可能会引用编译器使用的其他信息..但我不确定。有一件事让我想到:我在不同的数据集上运行valgrind(由程序处理),具有不同的大小和仍然可达的字节数总是相同的。

提前致谢!

1 个答案:

答案 0 :(得分:0)

  

我想弄清楚是什么导致Valgrind警告一些“仍然可以到达”的区块。

要求 Valgrind用--show-reachable=yes标志告诉你他们。如果您不想看到它们,请不要使用该标志。

现在,你的真正的问题可能是“什么是可到达的块,我应该关心吗?”。

答案:不是。仍然可以访问的块意味着程序可以 free()d如果它们想要它(它仍然可以找到==到达它们),它只是没有关注到这样做。

以下是生成“仍可访问”的程序示例:

int DoSomething()
{
  // Heap-allocated so as not to exhaust stack.
  // Allocated only once.
  static char *const error_buf = malloc(10000);
  return DoSomethingWithErrorBuf(error_buf);
}

int main()
{
  return DoSomething();
}