<目次>
(1) ValgrindのIndirectly Lostの意味や実際のサンプルをご紹介
(1-1) Valgrindの「Indirectly Lost」はどんな状況?
(1-2) 例1:サンプルプログラム
(1-3) 例1:Valgrindのメモリリーク確認結果
(1-4) 例2:サンプルプログラム
(1-5) 例2:Valgrindのメモリリーク確認結果
(1) ValgrindのIndirectly Lostの意味や実際のサンプルをご紹介
本記事ではValgrindにおけるメモリリークのカテゴリの1つである「Indirectly Lost」についてご紹介します。
(1-1) Valgrindの「Indirectly Lost」はどんな状況?
(1-2) 例1:サンプルプログラム
#include <iostream> using namespace std; int main(void){ //# gの三重ポインタを定義 //# int型のポインタのポインタの配列(1要素)のアドレスを割り当て int ***val = new int**; //# gのポインタの値に //# int型のポインタの配列(1要素)を格納 *val = new int*; //# gのポインタのポインタの値に //# int型の配列(1要素)を格納 **val = new int; //# gのポインタのポインタのポインタの値に99をセット ***val = 99; delete val; }
(1-3) 例1:Valgrindのメモリリーク確認結果
==31777== HEAP SUMMARY: ==31777== in use at exit: 12 bytes in 2 blocks ==31777== total heap usage: 3 allocs, 1 frees, 20 bytes allocated ==31777== ==31777== 4 bytes in 1 blocks are indirectly lost in loss record 1 of 2 ==31777== at 0x4C2B788: operator new(unsigned long) (vg_replace_malloc.c:417) ==31777== by 0x400715: main (memorytest4.cpp:7) ==31777== ==31777== 12 (8 direct, 4 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2 ==31777== at 0x4C2B788: operator new(unsigned long) (vg_replace_malloc.c:417) ==31777== by 0x4006FD: main (memorytest4.cpp:6) ==31777== ==31777== LEAK SUMMARY: ==31777== definitely lost: 8 bytes in 1 blocks ==31777== indirectly lost: 4 bytes in 1 blocks ==31777== possibly lost: 0 bytes in 0 blocks ==31777== still reachable: 0 bytes in 0 blocks ==31777== suppressed: 0 bytes in 0 blocks ==31777== ==31777== For lists of detected and suppressed errors, rerun with: -s ==31777== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
(1-4) 例2:サンプルプログラム
#include <iostream> using namespace std; int main(void){ double **h = new double*[5]; h[0] = new double; h[1] = new double; h[2] = new double; h[3] = new double; h[4] = new double; }
(1-5) 例2:Valgrindのメモリリーク確認結果
==512== HEAP SUMMARY: ==512== in use at exit: 36 bytes in 4 blocks ==512== total heap usage: 4 allocs, 0 frees, 36 bytes allocated ==512== ==512== 4 bytes in 1 blocks are indirectly lost in loss record 1 of 4 ==512== at 0x4C2B788: operator new(unsigned long) (vg_replace_malloc.c:417) ==512== by 0x4006FD: main (memorytest_indirectly2.cpp:7) ==512== ==512== 4 bytes in 1 blocks are indirectly lost in loss record 2 of 4 ==512== at 0x4C2B788: operator new(unsigned long) (vg_replace_malloc.c:417) ==512== by 0x400716: main (memorytest_indirectly2.cpp:8) ==512== ==512== 4 bytes in 1 blocks are indirectly lost in loss record 3 of 4 ==512== at 0x4C2B788: operator new(unsigned long) (vg_replace_malloc.c:417) ==512== by 0x40072B: main (memorytest_indirectly2.cpp:9) ==512== ==512== 36 (24 direct, 12 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4 ==512== at 0x4C2C866: operator new[](unsigned long) (vg_replace_malloc.c:579) ==512== by 0x4006EF: main (memorytest_indirectly2.cpp:6) ==512== ==512== LEAK SUMMARY: ==512== definitely lost: 24 bytes in 1 blocks ==512== indirectly lost: 12 bytes in 3 blocks ==512== possibly lost: 0 bytes in 0 blocks ==512== still reachable: 0 bytes in 0 blocks ==512== suppressed: 0 bytes in 0 blocks ==512== ==512== For lists of detected and suppressed errors, rerun with: -s ==512== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)