「free忘れ」の編集履歴(バックアップ)一覧はこちら
「free忘れ」(2009/02/12 (木) 22:47:10) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
*目的
mtraceを使ってmemory leakを検出しfree忘れを見つける~
GNU C Liraryで実装されている。
* コード
buf2をfreeし忘れている
#include <stdio.h>
#include <malloc.h>
#include <mcheck.h> /* for check memory leak */
int
main()
{
char *buf1, *buf2;
mtrace();
buf1 = (char*)malloc(10);
buf2 = (char*)malloc(10);
free(buf1); /* free only buf1 */
muntrace();
return(0);
}
*実行
$ gcc -o free free.c -g
$ env MALLOC_TRACE=./free.dat ./free # MALLOC_TRACEでtrace結果を保存するfileを指定
$ mtrace free free.dat # trace fileを表示
Memory not freed:
-----------------
Address Size Caller
0x0804a388 0xa at /home/fuz/Desktop/linux_system_programming/free.c:13
13行目で確保したbuf2が開放されていない
13 buf2 = (char*)malloc(10);
*目的
mtraceを使ってmemory leakを検出しfree忘れを見つける~
Ulrich DrepperによってGNU C Libraryで実装されている。
* コード
buf2をfreeし忘れている
#include <stdio.h>
#include <malloc.h>
#include <mcheck.h> /* for check memory leak */
int
main()
{
char *buf1, *buf2;
mtrace();
buf1 = (char*)malloc(10);
buf2 = (char*)malloc(10);
free(buf1); /* free only buf1 */
muntrace();
return(0);
}
*実行
$ gcc -o free free.c -g
$ env MALLOC_TRACE=./free.dat ./free # MALLOC_TRACEでtrace結果を保存するfileを指定
$ mtrace free free.dat # trace fileを表示
Memory not freed:
-----------------
Address Size Caller
0x0804a388 0xa at /home/fuz/Desktop/linux_system_programming/free.c:13
13行目で確保したbuf2が開放されていない
13 buf2 = (char*)malloc(10);