スポンサーリンク

このドキュメントの内容は、以下の通りです。

メモリリークのないプログラムを動かしたときの実行例は、以下のとおりです。

valgrind --leak-check=full a.out
==20786== Memcheck, a memory error detector
==20786== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20786== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==20786== Command: a.out
==20786==
==20786==
==20786== HEAP SUMMARY:
==20786==     in use at exit: 0 bytes in 0 blocks
==20786==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==20786==
==20786== All heap blocks were freed -- no leaks are possible
==20786==
==20786== For counts of detected and suppressed errors, rerun with: -v
==20786== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

a.cc


メモリリークするプログラムの例です。

void
test (void)
{
	int *a = new int [2];

	a [ 2 ] = 0;	// メモリリーク
}

int
main (int argc, char *argv[])
{

	test ();

	exit (EXIT_SUCCESS);
}

コンパイル


コンパイルします。

g++ a.cc

実行例


% valgrind a.out
==20845== Memcheck, a memory error detector
==20845== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20845== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==20845== Command: a.out
==20845==
==20845== Invalid write of size 4
==20845==    at 0x80487DB: test() (in /tmp/v/a.out)
==20845==    by 0x8048805: main (in /tmp/v/a.out)
==20845==  Address 0x292038 is 0 bytes after a block of size 8 alloc'd
==20845==    at 0x59734: operator new[](unsigned int) (in /usr/local/lib/valgrind/vgpreload_memcheck-x86-freebsd.so)
==20845==    by 0x80487D1: test() (in /tmp/v/a.out)
==20845==    by 0x8048805: main (in /tmp/v/a.out)
==20845==
==20845==
==20845== HEAP SUMMARY:
==20845==     in use at exit: 8 bytes in 1 blocks
==20845==   total heap usage: 1 allocs, 0 frees, 8 bytes allocated
==20845==
==20845== LEAK SUMMARY:
==20845==    definitely lost: 8 bytes in 1 blocks
==20845==    indirectly lost: 0 bytes in 0 blocks
==20845==      possibly lost: 0 bytes in 0 blocks
==20845==    still reachable: 0 bytes in 0 blocks
==20845==         suppressed: 0 bytes in 0 blocks
==20845== Rerun with --leak-check=full to see details of leaked memory
==20845==
==20845== For counts of detected and suppressed errors, rerun with: -v
==20845== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)


--leak-check=full オプションを使うことで、メモリリークの詳細を確認することができます。

% valgrind --leak-check=full a.out 
==20850== Memcheck, a memory error detector
==20850== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20850== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==20850== Command: a.out
==20850==
==20850== Invalid write of size 4
==20850==    at 0x80487DB: test() (in /tmp/v/a.out)
==20850==    by 0x8048805: main (in /tmp/v/a.out)
==20850==  Address 0x292038 is 0 bytes after a block of size 8 alloc'd
==20850==    at 0x59734: operator new[](unsigned int) (in /usr/local/lib/valgrind/vgpreload_memcheck-x86-freebsd.so)
==20850==    by 0x80487D1: test() (in /tmp/v/a.out)
==20850==    by 0x8048805: main (in /tmp/v/a.out)
==20850==
==20850==
==20850== HEAP SUMMARY:
==20850==     in use at exit: 8 bytes in 1 blocks
==20850==   total heap usage: 1 allocs, 0 frees, 8 bytes allocated
==20850==
==20850== 8 bytes in 1 blocks are definitely lost in loss record 1 of 1
==20850==    at 0x59734: operator new[](unsigned int) (in /usr/local/lib/valgrind/vgpreload_memcheck-x86-freebsd.so)
==20850==    by 0x80487D1: test() (in /tmp/v/a.out)
==20850==    by 0x8048805: main (in /tmp/v/a.out)
==20850==
==20850== LEAK SUMMARY:
==20850==    definitely lost: 8 bytes in 1 blocks
==20850==    indirectly lost: 0 bytes in 0 blocks
==20850==      possibly lost: 0 bytes in 0 blocks
==20850==    still reachable: 0 bytes in 0 blocks
==20850==         suppressed: 0 bytes in 0 blocks
==20850==
==20850== For counts of detected and suppressed errors, rerun with: -v
==20850== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)


スポンサーリンク
スポンサーリンク
 
いつもシェア、ありがとうございます!


もっと情報を探しませんか?

関連記事

最近の記事

人気のページ

スポンサーリンク
 

過去ログ

2020 : 01 02 03 04 05 06 07 08 09 10 11 12
2019 : 01 02 03 04 05 06 07 08 09 10 11 12
2018 : 01 02 03 04 05 06 07 08 09 10 11 12
2017 : 01 02 03 04 05 06 07 08 09 10 11 12
2016 : 01 02 03 04 05 06 07 08 09 10 11 12
2015 : 01 02 03 04 05 06 07 08 09 10 11 12
2014 : 01 02 03 04 05 06 07 08 09 10 11 12
2013 : 01 02 03 04 05 06 07 08 09 10 11 12
2012 : 01 02 03 04 05 06 07 08 09 10 11 12
2011 : 01 02 03 04 05 06 07 08 09 10 11 12
2010 : 01 02 03 04 05 06 07 08 09 10 11 12
2009 : 01 02 03 04 05 06 07 08 09 10 11 12
2008 : 01 02 03 04 05 06 07 08 09 10 11 12
2007 : 01 02 03 04 05 06 07 08 09 10 11 12
2006 : 01 02 03 04 05 06 07 08 09 10 11 12
2005 : 01 02 03 04 05 06 07 08 09 10 11 12
2004 : 01 02 03 04 05 06 07 08 09 10 11 12
2003 : 01 02 03 04 05 06 07 08 09 10 11 12

サイト

Vim入門

C言語入門

C++入門

JavaScript/Node.js入門

Python入門

FreeBSD入門

Ubuntu入門

セキュリティ入門

パソコン自作入門

ブログ

トップ


プライバシーポリシー