0

我正在使用一些直接从磁盘文件中读取的原始代码。整个开始代码中只有一个 malloc 调用(我检查过这不是问题)。在我自己的代码中,我不使用 malloc、strdup 或任何其他堆调用。但是,运行 valgrind 并进行完整的泄漏检查,我仍然得到(下)。

我不认识它显示的任何文件。我在带有 Lion 的 Mac OSX 上运行它。这是否可能是系统库的问题,或者是他们解决此问题的另一种方法。我知道有些人会争辩说拥有可访问的内存是可以的,但我很想知道为什么任何内存仍然可以访问。

-

-90966-- REDIR: 0x3a46c7 (free) redirected to 0x55b8 (free)
==90966== 
==90966== HEAP SUMMARY:
==90966==     in use at exit: 6,383 bytes in 33 blocks
==90966==   total heap usage: 34 allocs, 1 frees, 6,899 bytes allocated
==90966== 
==90966== Searching for pointers to 33 not-freed blocks
==90966== Checked 684,888 bytes
==90966== 
==90966== 8 bytes in 1 blocks are still reachable in loss record 1 of 9
==90966==    at 0x5237: malloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==90966==    by 0x7FFF5FC11C2A: malloc (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC19C4E: operator new(unsigned long) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0817E: std::vector<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), std::allocator<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)> >::_M_insert_aux(__gnu_cxx::__normal_iterator<char const* (**)(dyld_image_states, unsigned int, dyld_image_info const*), std::vector<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), std::allocator<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)> > >, char const* (* const&)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC08260: std::vector<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), std::allocator<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)> >::insert(__gnu_cxx::__normal_iterator<char const* (**)(dyld_image_states, unsigned int, dyld_image_info const*), std::vector<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), std::allocator<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)> > >, char const* (* const&)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC054FA: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==90966==    by 0x2C1ACB: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==90966==    by 0x2C2ACB: _dyld_initializer (in /usr/lib/system/libdyld.dylib)
==90966==    by 0x1CFE73: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==90966==    by 0x7FFF5FC0FDA5: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0FAF1: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D2E3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966== 
==90966== 87 bytes in 7 blocks are still reachable in loss record 2 of 9
==90966==    at 0x5237: malloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==90966==    by 0x342358: strdup (in /usr/lib/system/libsystem_c.dylib)
==90966==    by 0x4E7802: _xpc_strdup (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EDCCF: _xpc_dictionary_insert (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EDE0E: xpc_dictionary_set_string (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EFE8C: _libxpc_initializer (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x1CFE7D: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==90966==    by 0x7FFF5FC0FDA5: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0FAF1: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D2E3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D27C: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0E0B6: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966== 
==90966== 88 bytes in 1 blocks are still reachable in loss record 3 of 9
==90966==    at 0x5237: malloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==90966==    by 0x2CA9B0: get_or_create_key_element (in /usr/lib/system/libkeymgr.dylib)
==90966==    by 0x2CABC9: _keymgr_get_and_lock_processwide_ptr_2 (in /usr/lib/system/libkeymgr.dylib)
==90966==    by 0x2CAC36: __keymgr_initializer (in /usr/lib/system/libkeymgr.dylib)
==90966==    by 0x1CFE6E: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==90966==    by 0x7FFF5FC0FDA5: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0FAF1: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D2E3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D27C: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0E0B6: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC034B7: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0760A: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**) (in /usr/lib/dyld)
==90966== 
==90966== 136 bytes in 1 blocks are still reachable in loss record 4 of 9
==90966==    at 0x58DE: calloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==90966==    by 0x4E78FA: _xpc_calloc (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4E81D4: _xpc_base_create (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4F1C32: _xpc_domain_create (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EFF1E: _libxpc_initializer (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x1CFE7D: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==90966==    by 0x7FFF5FC0FDA5: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0FAF1: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D2E3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D27C: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0E0B6: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC034B7: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==90966== 
==90966== 224 bytes in 7 blocks are still reachable in loss record 5 of 9
==90966==    at 0x5237: malloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==90966==    by 0x4E793D: _xpc_malloc (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EDCB4: _xpc_dictionary_insert (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EDE0E: xpc_dictionary_set_string (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EFE8C: _libxpc_initializer (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x1CFE7D: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==90966==    by 0x7FFF5FC0FDA5: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0FAF1: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D2E3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D27C: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0E0B6: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC034B7: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==90966== 
==90966== 520 bytes in 7 blocks are still reachable in loss record 6 of 9
==90966==    at 0x5237: malloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==90966==    by 0x342358: strdup (in /usr/lib/system/libsystem_c.dylib)
==90966==    by 0x4E7802: _xpc_strdup (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EEBF5: xpc_string_create (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EDDFD: xpc_dictionary_set_string (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EFE8C: _libxpc_initializer (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x1CFE7D: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==90966==    by 0x7FFF5FC0FDA5: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0FAF1: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D2E3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D27C: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0E0B6: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966== 
==90966== 608 bytes in 1 blocks are still reachable in loss record 7 of 9
==90966==    at 0x58DE: calloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==90966==    by 0x4E78FA: _xpc_calloc (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4E81D4: _xpc_base_create (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EDFDA: xpc_dictionary_create (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EFE63: _libxpc_initializer (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x1CFE7D: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==90966==    by 0x7FFF5FC0FDA5: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0FAF1: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D2E3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D27C: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0E0B6: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC034B7: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==90966== 
==90966== 616 bytes in 7 blocks are still reachable in loss record 8 of 9
==90966==    at 0x58DE: calloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==90966==    by 0x4E78FA: _xpc_calloc (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4E81D4: _xpc_base_create (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EEBEA: xpc_string_create (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EDDFD: xpc_dictionary_set_string (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x4EFE8C: _libxpc_initializer (in /usr/lib/system/libxpc.dylib)
==90966==    by 0x1CFE7D: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==90966==    by 0x7FFF5FC0FDA5: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0FAF1: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D2E3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0D27C: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966==    by 0x7FFF5FC0E0B6: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==90966== 
==90966== 4,096 bytes in 1 blocks are still reachable in loss record 9 of 9
==90966==    at 0x5237: malloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==90966==    by 0x34F3F7: __smakebuf (in /usr/lib/system/libsystem_c.dylib)
==90966==    by 0x345D19: __swsetup (in /usr/lib/system/libsystem_c.dylib)
==90966==    by 0x3466C3: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==90966==    by 0x34618D: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==90966==    by 0x34F2CF: printf (in /usr/lib/system/libsystem_c.dylib)
==90966==    by 0x1000014D0: main (in .//diskimageaccess)
==90966== 
==90966== LEAK SUMMARY:
==90966==    definitely lost: 0 bytes in 0 blocks
==90966==    indirectly lost: 0 bytes in 0 blocks
==90966==      possibly lost: 0 bytes in 0 blocks
==90966==    still reachable: 6,383 bytes in 33 blocks
==90966==         suppressed: 0 bytes in 0 blocks
==90966== 
==90966== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==90966== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

编辑:

我尝试将代码移到 Linux 机器上,它可以完美地编译和运行,没有任何内存错误。任何想法为什么会这样?

4

3 回答 3

1

好吧,如果 valgrind 告诉你,肯定有人在使用这些函数。如果您确定没有使用任何堆函数,那么您的一些依赖项正在使用malloc/calloc. 您使用哪些库?

于 2012-10-13T18:41:35.970 回答
1

访问的内存消息不是真正的问题,但可能会令人讨厌,因为它们会掩盖真正的问题(滚动失明)。内存由OS X XPC分配。也许您正在与程序中的 OS X 服务交谈。

对于我自己的开发,我使用 cleanup-function shut_up_valgrind()。这会在退出之前释放所有内容。我知道这是多余的,因为操作系统将释放进程使用的所有内存。但根据我的经验,它迫使我编写更健壮的代码,因为我必须考虑内存并发现许多隐藏得很好的内存分配错误。

现在,如果你有像 XPC 这样的依赖项,你可以寻找清理函数。试着找出答案。也许关闭某些东西就足够了。然后在退出函数之前调用清理shut_up_valgrind()函数。

于 2012-10-13T19:32:12.070 回答
0

无需担心仍然可以访问的内存。

您可以从 valgrind 日志文件中的调用堆栈清楚地看到,内存是通过内存函数从堆中分配的。即malloc()&calloc()

于 2012-10-13T19:21:45.377 回答