我正在使用一些直接從磁盤文件讀取的原始代碼。在整個開始代碼中只有一個malloc調用(我檢查了這不是問題)。在我自己的代碼中,我不使用malloc,strdup或任何其他堆調用。然而,運行valgrind完整的泄漏檢查我仍然得到(下圖)。C 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機器移動和它編譯和完美的順利運行,沒有任何內存錯誤。任何想法,爲什麼這是?
標準c庫之外沒有依賴關係。 – ZenBalance
就是這樣。它最終導致MacOSX printf庫發生大量內存泄漏,因爲我正在編譯的另一臺機器上的Linux庫很乾淨,並且不會導致任何內存泄漏。 – ZenBalance