Linuxメモ / Valgrind / リークチェック実験


Last update: 2010-08-06 (金) 15:04:52

Linuxメモ/Valgrind/リークチェック実験
Tag: Linux Valgrind

緑文字が入力コマンド。
桃文字が今回の変更箇所。


Valgrindリークチェック実験

実験1 (freeする変数の間違い)

free()する変数を間違った場合。
2回目のfree()時にp2とするところをfree()済みのp1を指定。
malloc()free()の数は一致している。

以下のソースをエディタで作成します。

$ vi teste1.c[ENTER]

#include <stdlib.h>

int main( void )
{
	char* p1 = (char*)NULL;
	char* p2 = (char*)NULL;

	p1 = (char*)malloc( 10 );
	p2 = (char*)malloc( 18 );

	free( (void*)p1 );
	p1 = (char*)NULL;

	free( (void*)p1 );
	p2 = (char*)NULL;

	return 0;
}

-gをつけてコンパイルします。

$ gcc -Wall -g -o teste1 teste1.c[ENTER]

valgrindを経由して./teste1を起動して表示を確認します。

$ valgrind --leak-check=full ./teste1[ENTER]

teste1チェック結果

==25479== Memcheck, a memory error detector.
==25479== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==25479== Using LibVEX rev 1658, a library for dynamic binary translation.
==25479== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==25479== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==25479== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==25479== For more details, rerun with: -v
==25479==
==25479==
==25479== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)
==25479== malloc/free: in use at exit: 18 bytes in 1 blocks.
==25479== malloc/free: 2 allocs, 1 frees, 28 bytes allocated.
==25479== For counts of detected errors, rerun with: -v
==25479== searching for pointers to 1 not-freed blocks.
==25479== checked 63,416 bytes.
==25479==
==25479== 18 bytes in 1 blocks are definitely lost in loss record 1 of 1
==25479==    at 0x4A05809: malloc (vg_replace_malloc.c:149)
==25479==    by 0x400507: main (teste1.c:9)
==25479==
==25479== LEAK SUMMARY:
==25479==    definitely lost: 18 bytes in 1 blocks.
==25479==      possibly lost: 0 bytes in 0 blocks.
==25479==    still reachable: 0 bytes in 0 blocks.
==25479==         suppressed: 0 bytes in 0 blocks.
==25479== Reachable blocks (those to which a pointer was found) are not shown.
==25479== To see them, rerun with: --show-reachable=yes

ちゃんとリークが検出されました。


コメント

コメントはありません。 コメント/Linuxメモ/Valgrind/リークチェック実験?

Online: 1


FrontPage

Soft

Tips


最新の20件

2021-12-21 2020-04-06 2020-03-10 2013-06-28 2013-11-13 2014-06-24

今日の20件

  • counter: 1214
  • today: 1
  • yesterday: 0
  • online: 1

edit