Ubuntu12.04に Valgrindをインストール
Linuxで SystemCモデルなど開発しているのであれば、
使ってない人はいないのではないでしょうか?(言い過ぎかもしれません)
せっかくなので、個人PCにも入れてみました。
インストール
$> sudo apt-get install valgrind
- インストールログ
$> sudo apt-get install valgrind パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下の特別パッケージがインストールされます: libc6-dbg 提案パッケージ: kcachegrind alleyoop valkyrie 以下のパッケージが新たにインストールされます: libc6-dbg valgrind アップグレード: 0 個、新規インストール: 2 個、削除: 0 個、保留: 37 個。 17.5 MB のアーカイブを取得する必要があります。 この操作後に追加で 63.3 MB のディスク容量が消費されます。 続行しますか [Y/n]? Y 取得:1 http://jp.archive.ubuntu.com/ubuntu/ precise-updates/main libc6-dbg i386 2.15-0ubuntu10.3 [2,575 kB] 取得:2 http://jp.archive.ubuntu.com/ubuntu/ precise-proposed/main valgrind i386 1:3.7.0-0ubuntu3.1 [15.0 MB] 17.5 MB を 33秒 で取得しました (524 kB/s) 以前に未選択のパッケージ libc6-dbg を選択しています。 (データベースを読み込んでいます ... 現在 610219 個のファイルとディレクトリがインストールされています。) (.../libc6-dbg_2.15-0ubuntu10.3_i386.deb から) libc6-dbg を展開しています... 以前に未選択のパッケージ valgrind を選択しています。 (.../valgrind_1%3a3.7.0-0ubuntu3.1_i386.deb から) valgrind を展開しています... man-db のトリガを処理しています ... libc6-dbg (2.15-0ubuntu10.3) を設定しています ... valgrind (1:3.7.0-0ubuntu3.1) を設定しています ... $> valgrind --version valgrind-3.7.0
実行
適当にサンプル作って試してみたいと思います。
- main.cpp
#include <stdio.h> struct hoge { int a; }; int main(int argc, char const* argv[]) { hoge *tmp = new hoge; tmp->a = 1; printf("tmp->a = %d\n", tmp->a); // delete tmp; return 0; // メモリリーク }
- 実行結果
$> valgrind --leak-check=full ./a.out ==6207== Memcheck, a memory error detector ==6207== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==6207== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==6207== Command: ./a.out ==6207== tmp->a = 1 ==6207== ==6207== HEAP SUMMARY: ==6207== in use at exit: 4 bytes in 1 blocks ==6207== total heap usage: 1 allocs, 0 frees, 4 bytes allocated ==6207== ==6207== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==6207== at 0x402B9B4: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==6207== by 0x80484F8: main (main.cpp:9) ==6207== ==6207== LEAK SUMMARY: ==6207== definitely lost: 4 bytes in 1 blocks ==6207== indirectly lost: 0 bytes in 0 blocks ==6207== possibly lost: 0 bytes in 0 blocks ==6207== still reachable: 0 bytes in 0 blocks ==6207== suppressed: 0 bytes in 0 blocks ==6207== ==6207== For counts of detected and suppressed errors, rerun with: -v ==6207== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
はい。検出できていますね。
deleteのコメントを外すと Errorは解消されます。