<目次>
(1) ValgrindのStill Reachableの意味や実際のサンプルをご紹介
(1-1) Valgrindの「Still Reachable」はどんな状況?
(1-2) 例1:サンプルプログラム(still reachable発生版)
(1-3) 例1:Valgrindのメモリリーク確認結果
(1-4) 例2:サンプルプログラム(still reachable解消版)
(1) ValgrindのStill Reachableの意味や実際のサンプルをご紹介
本記事ではValgrindにおけるメモリリークのカテゴリの1つである「Still Reachable」についてご紹介します。
(1-1) Valgrindの「Still Reachable」はどんな状況?
(1-2) 例1:サンプルプログラム(still reachable発生版)
#include <stdlib.h> char *x1 = new char[3]; int main(void) { return 0; }
(1-3) 例1:Valgrindのメモリリーク確認結果
==12936== HEAP SUMMARY: ==12936== in use at exit: 100 bytes in 1 blocks ==12936== total heap usage: 1 allocs, 0 frees, 100 bytes allocated ==12936== ==12936== 100 bytes in 1 blocks are still reachable in loss record 1 of 1 ==12936== at 0x4C2C866: operator new[](unsigned long) (vg_replace_malloc.c:579) ==12936== by 0x4005BE: __static_initialization_and_destruction_0(int, int) (memorytest2.cpp:4) ==12936== by 0x4005DA: _GLOBAL__sub_I_x1 (memorytest2.cpp:9) ==12936== by 0x40062C: __libc_csu_init (in /tmp_rainbow/memorytest2) ==12936== by 0x567D364: (below main) (libc-start.c:225) ==12936== ==12936== LEAK SUMMARY: ==12936== definitely lost: 0 bytes in 0 blocks ==12936== indirectly lost: 0 bytes in 0 blocks ==12936== possibly lost: 0 bytes in 0 blocks ==12936== still reachable: 100 bytes in 1 blocks ==12936== suppressed: 0 bytes in 0 blocks
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ../チェック対象プログラム
(1-4) 例2:サンプルプログラム(still reachable解消版)
#include <stdlib.h> char *x1 = new char[3]; int main(void) { delete []x1; return 0; }
==11328== HEAP SUMMARY: ==11328== in use at exit: 0 bytes in 0 blocks ==11328== total heap usage: 1 allocs, 1 frees, 3 bytes allocated ==11328== ==11328== All heap blocks were freed -- no leaks are possible ==11328== ==11328== For lists of detected and suppressed errors, rerun with: -s ==11328== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)