Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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かけてみるのはおすすめです。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした