Linuxメモ / Valgrind / リークチェック方法


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

Linuxメモ/Valgrind/リークチェック方法
Tag: Linux Valgrind

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


Valgrindリークチェック方法

以下のコマンド入力でメモリリークをチェックする。

$ valgrind --leak-check=full COMMAND[ENTER]

メモリリーク無の例

最初はメモリリークのないパターンで表示を見ておきます。
以下のソースをエディタで作成します。

$ vi test1.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*)p2 );
	p2 = (char*)NULL;

	return 0;
}

普通にコンパイルします。

$ gcc -Wall -o test1 test1.c[ENTER]

valgrindを経由してtest1を起動して表示を確認します。

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

test1チェック結果

==24791== Memcheck, a memory error detector.
==24791== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==24791== Using LibVEX rev 1658, a library for dynamic binary translation.
==24791== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==24791== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==24791== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==24791== For more details, rerun with: -v
==24791==
==24791==
==24791== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)
==24791== malloc/free: in use at exit: 0 bytes in 0 blocks.
==24791== malloc/free: 2 allocs, 2 frees, 28 bytes allocated.
==24791== For counts of detected errors, rerun with: -v
==24791== All heap blocks were freed -- no leaks are possible.

メモリリーク有の例

次にメモリリークがある場合の表示を確認します。
以下のソースをエディタで作成します。

$ vi test2.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*)p2 );
	p2 = (char*)NULL;

	return 0;
}

普通にコンパイルします。

$ gcc -Wall -o test2 test2.c[ENTER]

valgrindを経由してtest2を起動して表示を確認します。

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

test2チェック結果

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

メモリリークがあるファイル関数までは特定してくれているようです。
行番号も表示させるためにデバッグオプション-gをつけてコンパイルしなおします。

$ gcc -Wall -g -o test2g test2.c[ENTER]

valgrindを経由してtest2gを起動して表示を確認します。

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

test2gチェック結果

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

行番号も表示されるようになりました。
メモリリークした領域をmalloc()している行を表示しています。

ちなみにメモリリークがない場合はデバッグオプションの有無は表示結果には関係ないようです。

メモリに問題がなければ常時、デバッグオプションを付けておきましょう。
問題がある場合でも、リリースのぎりぎりまでデバッグオプションを付けておきましょう。

制約

★静的データやスタックの境界違反は検出できない。

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

$ vi test3.c[ENTER]

char pg[10];

int main( void )
{
	char ps[10];

	pg[10] = '\0';
	ps[10] = '\0';

	return 0;
}

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

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

valgrindを経由してtest3を起動して表示を確認します。

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

test3チェック結果

==25316== Memcheck, a memory error detector.
==25316== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==25316== Using LibVEX rev 1658, a library for dynamic binary translation.
==25316== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==25316== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==25316== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==25316== For more details, rerun with: -v
==25316==
==25316==
==25316== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)
==25316== malloc/free: in use at exit: 0 bytes in 0 blocks.
==25316== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==25316== For counts of detected errors, rerun with: -v
==25316== All heap blocks were freed -- no leaks are possible.

エラーは検出されていません。

これに対して、ヒープの境界違反は検出できます。
以下のソースをエディタで作成します。

$ vi test4.c[ENTER]

#include <stdlib.h>

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

	p1 = (char*)malloc( 10 );

	p1[10] = '\0';

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

	return 0;
}

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

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

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

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

test4チェック結果

==25346== Memcheck, a memory error detector.
==25346== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==25346== Using LibVEX rev 1658, a library for dynamic binary translation.
==25346== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==25346== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==25346== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==25346== For more details, rerun with: -v
==25346==
==25346== Invalid write of size 1
==25346==    at 0x4004FE: main (test4.c:9)
==25346==  Address 0x4C1F03A is 0 bytes after a block of size 10 alloc'd
==25346==    at 0x4A05809: malloc (vg_replace_malloc.c:149)
==25346==    by 0x4004F1: main (test4.c:7)
==25346==
==25346== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 1)
==25346== malloc/free: in use at exit: 0 bytes in 0 blocks.
==25346== malloc/free: 1 allocs, 1 frees, 10 bytes allocated.
==25346== For counts of detected errors, rerun with: -v
==25346== All heap blocks were freed -- no leaks are possible.

コメント

コメントはありません。 コメント/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

今日の6件

  • counter: 13946
  • today: 1
  • yesterday: 1
  • online: 1

edit