访问次数
我的另一个总结性博客: todototry 大米粒

内存泄露检测之ccmalloc

ccmalloc是常用的内存检测工具之一,适用于Linux环境。

使用说明

官方例子:

例1:

#include <stdio.h>

void Leak(char *inStr)
{
char *str = (char *) malloc(strlen(inStr));
memcpy(str, inStr, strlen(inStr));
}

char *AvoidLeak(char *inStr)
{
char *str = (char *) malloc(strlen(inStr));
memcpy(str, inStr, strlen(inStr));
return str;
}

int main()
{
char *str;

Leak(
"This leaks 19 bytes");
str
= AvoidLeak("This is not a 26 byte leak");
free(str);
str
= AvoidLeak("12 byte leak");
exit(
0);
}

结果:

  

.--------------------------------------------------------------------------.
|================ ccmalloc-0.3.8 (C) 1997-2001 Armin Biere ================|
+--------------------------------------------------------------------------+
| executable = /home/faculty/donahoo/public_html/tools/ccmalloc/a.out |
| startup file = .ccmalloc (but not found) |
| log file = stderr |
| start time = Wed Dec 19 09:57:16 2001 |
| operating system = Linux 2.4.2-2smp i686 on earth.ecs.baylor.edu |
+--------------------------------------------------------------------------+
| only-count = 0 keep-deallocated-data = 0 |
| check-interval = 0 check-free-space = 0 |
| check-start = 0 file-info = 1 |
| chain-length = 0 additional-line = 1 |
| check-underwrites = 0 print-addresses = 0 |
| check-overwrites = 0 print-on-one-line = 0 |
| sort-by-wasted = 1 sort-by-size = 1 |
| # only-log-chain = 0 continue = 0 |
| # dont-log-chain = 0 statistics = 0 |
| debug = 0 library-chains = 0 |
| load-dynlibs = 0 align-8-byte = 0 |
| only-wasting-alloc= 1 |
`
--------------------------------------------------------------------------'

.
---------------.
|ccmalloc report|
=======================================================
| total # of| allocated | deallocated | garbage |
+-----------+-------------+-------------+-------------+
| bytes| 57 | 26 | 31 |
+-----------+-------------+-------------+-------------+
|allocations| 3 | 1 | 2 |
+-----------------------------------------------------+
| number of checks: 1 |
| number of counts: 4 |
| retrieving function names for addresses ... done. |
| reading file info from gdb ... done. |
| sorting by number of not reclaimed bytes ... done. |
| number of call chains: 2 |
| number of ignored call chains: 0 |
| number of reported call chains: 2 |
| number of internal call chains: 2 |
| number of library call chains: 0 |
=======================================================
|
* 61.3% = 19 Bytes of garbage allocated in 1 allocation
| |
| | 0x40047306 in <???>
| |
| | 0x080493eb in <main>
| | at test1.c:20
| |
| | 0x0804935c in <Leak>
| | at test1.c:5
| |
| `-----> 0x08052fb7 in <malloc>
| at src/wrapper.c:318
|
* 38.7% = 12 Bytes of garbage allocated in 1 allocation
| |
| | 0x40047306 in <???>
| |
| | 0x0804941e in <main>
| | at test1.c:23
| |
| | 0x080493a4 in <AvoidLeak>
| | at test1.c:11
| |
| `-----> 0x08052fb7 in <malloc>
| at src/wrapper.c:318
|
`
------------------------------------------------------

  

没看明白,继续再看例2

例2:

/* Test from creator of ccmalloc (http://iseran.ira.uka.de/~armin/ccmalloc/) */
#include
<stdio.h>
#include
<stdlib.h>

static char *mkstr(char *s)
{
return strcpy(malloc(strlen(s)+1), s);
}

void f() {
mkstr(
"asdfasdf");
}

void g()
{
int i;
char *a[100];

for(i=0; i<100; i++)
a[i]
= mkstr("in f");

free(a[
2]);
free(a[
0]);
}

int main()
{
char *a = mkstr("Hallo"), *b;

(
void) mkstr("Test");

f();

b
= mkstr("Test");

g();

a[
6]=0;

free(b);
free(a);

exit(
0);
return 1;
}

使用CCMalloc结果:

.--------------------------------------------------------------------------.
|================ ccmalloc-0.3.8 (C) 1997-2001 Armin Biere ================|
+--------------------------------------------------------------------------+
| executable = /home/faculty/donahoo/public_html/tools/ccmalloc/a.out |
| startup file = .ccmalloc (but not found) |
| log file = stderr |
| start time = Wed Dec 19 10:05:49 2001 |
| operating system = Linux 2.4.2-2smp i686 on earth.ecs.baylor.edu |
+--------------------------------------------------------------------------+
| only-count = 0 keep-deallocated-data = 0 |
| check-interval = 0 check-free-space = 0 |
| check-start = 0 file-info = 1 |
| chain-length = 0 additional-line = 1 |
| check-underwrites = 0 print-addresses = 0 |
| check-overwrites = 0 print-on-one-line = 0 |
| sort-by-wasted = 1 sort-by-size = 1 |
| # only-log-chain = 0 continue = 0 |
| # dont-log-chain = 0 statistics = 0 |
| debug = 0 library-chains = 0 |
| load-dynlibs = 0 align-8-byte = 0 |
| only-wasting-alloc= 1 |
`
--------------------------------------------------------------------------'

.
---------------.
|ccmalloc report|
=======================================================
| total # of| allocated | deallocated | garbage |
+-----------+-------------+-------------+-------------+
| bytes| 525 | 21 | 504 |
+-----------+-------------+-------------+-------------+
|allocations| 104 | 4 | 100 |
+-----------------------------------------------------+
| number of checks: 1 |
| number of counts: 108 |
| retrieving function names for addresses ... done. |
| reading file info from gdb ... done. |
| sorting by number of not reclaimed bytes ... done. |
| number of call chains: 3 |
| number of ignored call chains: 0 |
| number of reported call chains: 3 |
| number of internal call chains: 3 |
| number of library call chains: 0 |
=======================================================
|
* 97.2% = 490 Bytes of garbage allocated in 98 allocations
| |
| | 0x40047306 in <???>
| |
| | 0x0804944a in <main>
| | at test2.c:36
| |
| | 0x080493b9 in <g>
| | at test2.c:20
| |
| | 0x08049360 in <mkstr>
| | at test2.c:7
| |
| `-----> 0x08053007 in <malloc>
| at src/wrapper.c:318
|
| 1.8% = 9 Bytes of garbage allocated in 1 allocation
| |
| | 0x40047306 in <???>
| |
| | 0x08049430 in <main>
| | at test2.c:32
| |
| | 0x0804938b in <f>
| | at test2.c:11
| |
| | 0x08049360 in <mkstr>
| | at test2.c:7
| |
| `-----> 0x08053007 in <malloc>
| at src/wrapper.c:318
|
| 0.10% = 5 Bytes of garbage allocated in 1 allocation
| |
| | 0x40047306 in <???>
| |
| | 0x08049428 in <main>
| | at test2.c:30
| |
| | 0x08049360 in <mkstr>
| | at test2.c:7
| |
| `-----> 0x08053007 in <malloc>
| at src/wrapper.c:318
|
`
------------------------------------------------------

  

posted @ 2011-09-05 18:29  fandyst  阅读(2819)  评论(0编辑  收藏  举报