valgrind でメモリリークの誤検出が起きる場合、suppression を指定する
valgrind は、問題がない場合でも誤検出することがママある。 そのような場合、suppressions ファイルを生成すればよい。
たとえば、dlopen()
は内部でエラーメッセージ保存用のバッファを calloc で確保するが、このバッファは開放されないが、これはバグではないということになっている。See. https://sourceware.org/bugzilla/show_bug.cgi?id=14015
このような場合、以下のようにして、--gen-suppressions=all
として、suppressions 設定をダンプさせることができる。
$ make -j 4 && valgrind --gen-suppressions=all --leak-check=full --show-leak-kinds=all ./a.out
make: Nothing to be done for `all'.
==829== Memcheck, a memory error detector
==829== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==829== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==829== Command: ./a.out
==829==
==829==
==829== HEAP SUMMARY:
==829== in use at exit: 32 bytes in 1 blocks
==829== total heap usage: 437 allocs, 436 frees, 117,923 bytes allocated
==829==
==829== 32 bytes in 1 blocks are still reachable in loss record 1 of 1
==829== at 0x4C2B974: calloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==829== by 0x4E3668F: _dlerror_run (dlerror.c:141)
==829== by 0x4E360C0: dlopen@@GLIBC_2.2.5 (dlopen.c:87)
==829== by 0x407599: pone_compile_node (main.c:771)
==829== by 0x407851: main (main.c:845)
==829==
{
<insert_a_suppression_name_here>
Memcheck:Leak
match-leak-kinds: reachable
fun:calloc
fun:_dlerror_run
fun:dlopen@@GLIBC_2.2.5
fun:pone_compile_node
fun:main
}
==829== LEAK SUMMARY:
==829== definitely lost: 0 bytes in 0 blocks
==829== indirectly lost: 0 bytes in 0 blocks
==829== possibly lost: 0 bytes in 0 blocks
==829== still reachable: 32 bytes in 1 blocks
==829== suppressed: 0 bytes in 0 blocks
==829==
==829== For counts of detected and suppressed errors, rerun with: -v
==829== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
この内容を t/myapp.supp などに保存する。そして、このファイルを指定して起動すると、以下のように、不要な警告をだまらせることができる。
$ make -j 4 && valgrind --suppressions=t/myapp.supp --leak-check=full --show-leak-kinds=all ./a.out
make: Nothing to be done for `all'.
==862== Memcheck, a memory error detector
==862== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==862== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==862== Command: ./a.out
==862==
==862==
==862== HEAP SUMMARY:
==862== in use at exit: 32 bytes in 1 blocks
==862== total heap usage: 437 allocs, 436 frees, 117,923 bytes allocated
==862==
==862== LEAK SUMMARY:
==862== definitely lost: 0 bytes in 0 blocks
==862== indirectly lost: 0 bytes in 0 blocks
==862== possibly lost: 0 bytes in 0 blocks
==862== still reachable: 0 bytes in 0 blocks
==862== suppressed: 32 bytes in 1 blocks
==862==
==862== For counts of detected and suppressed errors, rerun with: -v
==862== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)