0

ディスクファイルから直接読み取る元のコードを使用しています。最初のコード全体で malloc 呼び出しが 1 つだけあります (それが問題ではないことを確認しました)。私自身のコードでは、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 に答える