search
LoginSignup
74

More than 5 years have passed since last update.

posted at

updated at

valgrindでC/C++のメモリリークを発見する

c言語やc++のプログラムのメモリリークをチェックするのに、valgrindが便利です。

install

sudo apt-get install valgrind

brew install valgrind
で入ります。

参考: Yosemiteでvalgrindを入れる - Qiita

使ってみる

例えば次のような、メモリリークやダブルフリーをしてるコードがあるとします。

#include <stdlib.h>

int main() {
  char* test1 = (char*)malloc(sizeof(char) * 4);
  char* test2 = (char*)malloc(sizeof(char) * 4);

  free(test2);
  free(test2);
}

上の例では、test1が解放されておらず, test2が2回フリーされてる。

ビルドします。

gcc sample.c -g

valgrindを使って実行します。

valgrind --leak-check=full ./a.out

すると、以下のように出力されます。

==23525== Invalid free() / delete / delete[] / realloc()
==23525==    at 0x1000089DF: free (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==23525==    by 0x100000F4F: main (sample.c:8)
==23525==  Address 0x10081f510 is 0 bytes inside a block of size 4 free'd
==23525==    at 0x1000089DF: free (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==23525==    by 0x100000F46: main (sample.c:7)
==23525==
==23525==
==23525== HEAP SUMMARY:
==23525==     in use at exit: 34,997 bytes in 426 blocks
==23525==   total heap usage: 507 allocs, 82 frees, 41,129 bytes allocated
==23525==
==23525== 4 bytes in 1 blocks are definitely lost in loss record 1 of 81
==23525==    at 0x10000859B: malloc (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==23525==    by 0x100000F26: main (sample.c:4)
==23525==
==23525== LEAK SUMMARY:
==23525==    definitely lost: 4 bytes in 1 blocks
==23525==    indirectly lost: 0 bytes in 0 blocks
==23525==      possibly lost: 0 bytes in 0 blocks
==23525==    still reachable: 0 bytes in 0 blocks
==23525==         suppressed: 34,993 bytes in 425 blocks
==23525==
==23525== For counts of detected and suppressed errors, rerun with: -v
==23525== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 17 from 17)

上記のうち、

==23525== Invalid free() / delete / delete[] / realloc()
==23525==    at 0x1000089DF: free (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==23525==    by 0x100000F4F: main (sample.c:8)
==23525==  Address 0x10081f510 is 0 bytes inside a block of size 4 free'd
==23525==    at 0x1000089DF: free (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==23525==    by 0x100000F46: main (sample.c:7)

の部分は、7行目でfreeしたのに8行目でもfreeしてしまっていることを示しています。

そして、

==23525== 4 bytes in 1 blocks are definitely lost in loss record 1 of 81
==23525==    at 0x10000859B: malloc (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==23525==    by 0x100000F26: main (sample.c:4)

では4行目のmallocで確保した領域が解放されていないことを示しています。

という感じでお手軽にチェックできるので、とりあえずvalgrindかけてみるのはおすすめです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
74