内存泄露检测之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
|
`
------------------------------------------------------

  

转载于:https://www.cnblogs.com/ToDoToTry/archive/2011/09/05/2167834.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值