RSS

valgrind

A memory debugger and profiler

適当にメモリリークを起こすコードを書く。

#include <stdlib.h>

int
main(void)
{
malloc(100);
return 0;
}

次に -g 付きでコンパイルしてからvalgrindを通して実行する。

$ valgrind --leak-check=full ./a.out 
==28368== Memcheck, a memory error detector.
==28368== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==28368== Using LibVEX rev 1732, a library for dynamic binary translation.
==28368== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==28368== Using valgrind-3.2.3-Debian, a dynamic binary instrumentation framework.
==28368== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==28368== For more details, rerun with: -v
==28368==
==28368==
==28368== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1)
==28368== malloc/free: in use at exit: 100 bytes in 1 blocks.
==28368== malloc/free: 1 allocs, 0 frees, 100 bytes allocated.
==28368== For counts of detected errors, rerun with: -v
==28368== searching for pointers to 1 not-freed blocks.
==28368== checked 59,080 bytes.
==28368==
==28368== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
==28368== at 0x40244B0: malloc (vg_replace_malloc.c:149)
==28368== by 0x8048370: main (leak.c:6)
==28368==
==28368== LEAK SUMMARY:
==28368== definitely lost: 100 bytes in 1 blocks.
==28368== possibly lost: 0 bytes in 0 blocks.
==28368== still reachable: 0 bytes in 0 blocks.
==28368== suppressed: 0 bytes in 0 blocks.

てな具合です。

   - Accesses memory it shouldn't (areas not yet allocated, areas that have
been freed, areas past the end of heap blocks, inaccessible areas of
the stack).

- Uses uninitialised values in dangerous ways.

- Leaks memory.

- Does bad frees of heap blocks (double frees, mismatched frees).

- Passes overlapping source and destination memory blocks to memcpy() and
related functions.

 などができるとのこと。