C valgrind仍然可以访问内存泄漏但没有使用堆?

时间:2012-10-13 17:50:55

标签: c memory-leaks valgrind

我正在使用一些直接从磁盘文件中读取的原始代码。整个开始代码中只有一个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机器上,它编译并运行得非常干净,没有任何内存错误。任何想法为什么会这样?

3 个答案:

答案 0 :(得分:1)

好吧,如果valgrind告诉你,有人必须使用这些功能。如果您确定没有使用任何堆函数,则某些依赖项使用malloc/calloc。您使用哪些库?

答案 1 :(得分:1)

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

对于我自己的开发,我使用了一个清理函数shut_up_valgrind()。这会在退出之前释放所有内容。我知道这是多余的,因为操作系统将释放进程使用的所有内存。但根据我的经验,它迫使我编写更强大的代码,因为我不得不考虑内存并发现许多隐藏的内存分配错误。

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

答案 2 :(得分:0)

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

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