<目次>
(1) ValgrindのDefinetely Lostの意味や実際のサンプルをご紹介
(1-1) Valgrindの「Definetely Lost」はどんな状況?
(1-2) 例1:サンプルプログラム
(1-3) 例1:Valgrindのメモリリーク確認結果
(1-4) 例2:サンプルプログラム(Definetely Lost)
(1) ValgrindのDefinetely Lostの意味や実際のサンプルをご紹介
本記事ではValgrindにおけるメモリリークのカテゴリの1つである「Definetely Lost」についてご紹介します。
(1-1) Valgrindの「Definetely Lost」はどんな状況?
(1-2) 例1:サンプルプログラム
#include <stdlib.h> int main() { char *x1 = new char[3]; return 0; }
(1-3) 例1:Valgrindのメモリリーク確認結果
==13514== HEAP SUMMARY: ==13514== in use at exit: 100 bytes in 1 blocks ==13514== total heap usage: 1 allocs, 0 frees, 100 bytes allocated ==13514== ==13514== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==13514== at 0x4C2C866: operator new[](unsigned long) (vg_replace_malloc.c:579) ==13514== by 0x40059E: main (memorytest.cpp:5) ==13514== ==13514== LEAK SUMMARY: ==13514== definitely lost: 100 bytes in 1 blocks ==13514== indirectly lost: 0 bytes in 0 blocks ==13514== possibly lost: 0 bytes in 0 blocks ==13514== still reachable: 0 bytes in 0 blocks ==13514== suppressed: 0 bytes in 0 blocks
(図131)
(1-4) 例2:サンプルプログラム(Definetely Lost)
#include <stdlib.h> int main() { char *x1 = new char[100]; delete []x1; return 0; }
(図141)
メモリ解放を追記すると「still reachable」が表示されなくなりました。
(出力サンプル)
==32059== HEAP SUMMARY: ==32059== in use at exit: 0 bytes in 0 blocks ==32059== total heap usage: 1 allocs, 1 frees, 100 bytes allocated ==32059== ==32059== All heap blocks were freed -- no leaks are possible ==32059== ==32059== For lists of detected and suppressed errors, rerun with: -s ==32059== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
(図142)