我们一直在使用dmalloc
我们的工具集来验证我们的核心库是否没有内存泄漏。但是,最近我们发现使用printf
orfgetc
会导致 dmalloc 在 dmalloc.log 中抛出以下警告。
not freed: '0x7f2e20d36808|s1' (1182 bytes) from 'unknown'
为了演示这个问题,下面是一个我们用来重现错误的非常简单的程序:
#include <stdio.h>
#include <stdlib.h>
#include "dmalloc.h"
void main() {
dmalloc_debug_setup("log-stats,log-non-free,check-fence,log=dmalloc.log");
printf("Hello World\n");
fgetc(stdin);
}
编译程序:
gcc test.c -ldmalloc -g -o test
运行,我得到以下结果:
1630587465: 2: Dmalloc version '5.5.2' from 'http://dmalloc.com/'
1630587465: 2: flags = 0x403, logfile 'dmalloc.log'
1630587465: 2: interval = 0, addr = 0, seen # = 0, limit = 0
1630587465: 2: threads enabled, lock-on = 0, lock-init = 2
1630587465: 2: starting time = 1630587464
1630587465: 2: process pid = 2079
1630587465: 2: Dumping Chunk Statistics:
1630587465: 2: basic-block 4096 bytes, alignment 8 bytes
1630587465: 2: heap address range: 0x7f343a6e0000 to 0x7f343a712000, 204800 bytes
1630587465: 2: user blocks: 4 blocks, 8192 bytes (33%)
1630587465: 2: admin blocks: 2 blocks, 8192 bytes (33%)
1630587465: 2: total blocks: 6 blocks, 24576 bytes
1630587465: 2: heap checked 0
1630587465: 2: alloc calls: malloc 2, calloc 0, realloc 0, free 0
1630587465: 2: alloc calls: recalloc 0, memalign 0, posix_memalign 0, valloc 0
1630587465: 2: alloc calls: new 0, delete 0
1630587465: 2: current memory in use: 8192 bytes (2 pnts)
1630587465: 2: total memory allocated: 8192 bytes (2 pnts)
1630587465: 2: max in use at one time: 8192 bytes (2 pnts)
1630587465: 2: max alloced with 1 call: 4096 bytes
1630587465: 2: max unused memory space: 8192 bytes (50%)
1630587465: 2: top 10 allocations:
1630587465: 2: total-size count in-use-size count source
1630587465: 2: 0 0 0 0 Total of 0
1630587465: 2: Dumping Not-Freed Pointers Changed Since Start:
1630587465: 2: not freed: '0x7f343a6f0008|s1' (4096 bytes) from 'unknown'
1630587465: 2: not freed: '0x7f343a710008|s1' (4096 bytes) from 'unknown'
1630587465: 2: total-size count source
1630587465: 2: 0 0 Total of 0
1630587465: 2: ending time = 1630587465, elapsed since start = 0:00:01
删除fgetc
我只收到一个警告,内存没有被释放。
1630587578: 1: Dumping Not-Freed Pointers Changed Since Start:
1630587578: 1: not freed: '0x7f939f4e0008|s1' (4096 bytes) from 'unknown'
1630587578: 1: total-size count source
1630587578: 1: 0 0 Total of 0
删除printf
然后我得到:
1630587655: 0: Dumping Not-Freed Pointers Changed Since Start:
1630587655: 0: memory table is empty
1630587655: 0: ending time = 1630587655, elapsed since start = 0:00:00
这里使用的gcc
版本是 9.3.0,dmalloc 是 5.5.2,当使用他们的包管理器安装时,它是最新的 Linux 发行版附带的。
如果这是一个已知问题,或者我在这里缺少一些明显的设置,任何有经验的人都dmalloc
可以指出我吗?dmalloc
更新
- 编译时也会出现这种情况
g++
。 - 有人指出库 5.5.2 已经很老了。我已经构建了最新版本 5.6.5 并使用 gcc 11.2.0 进行了测试,但问题仍然存在。
1630589888: 2: Dmalloc version '5.6.5' from 'http://dmalloc.com/'
1630589888: 2: flags = 0x403, logfile 'dmalloc.log'
1630589888: 2: interval = 0, addr = 0x0, seen # = 0, limit = 0
1630589888: 2: starting time = 1630589886
1630589888: 2: process pid = 11185
1630589888: 2: Dumping Chunk Statistics:
1630589888: 2: basic-block 4096 bytes, alignment 8 bytes
1630589888: 2: heap address range: 0x7ff82bd98000 to 0x7ff82bd9b000, 12288 bytes
1630589888: 2: user blocks: 1 blocks, 2048 bytes (16%)
1630589888: 2: admin blocks: 2 blocks, 8192 bytes (67%)
1630589888: 2: total blocks: 3 blocks, 12288 bytes
1630589888: 2: heap checked 0
1630589888: 2: alloc calls: malloc 2, calloc 0, realloc 0, free 0
1630589888: 2: alloc calls: recalloc 0, memalign 0, valloc 0
1630589888: 2: alloc calls: new 0, delete 0
1630589888: 2: current memory in use: 2048 bytes (2 pnts)
1630589888: 2: total memory allocated: 2048 bytes (2 pnts)
1630589888: 2: max in use at one time: 2048 bytes (2 pnts)
1630589888: 2: max alloced with 1 call: 1024 bytes
1630589888: 2: max unused memory space: 2048 bytes (50%)
1630589888: 2: top 10 allocations:
1630589888: 2: total-size count in-use-size count source
1630589888: 2: 0 0 0 0 Total of 0
1630589888: 2: Dumping Not-Freed Pointers Changed Since Start:
1630589888: 2: not freed: '0x7ff82bd9a008|s1' (1024 bytes) from 'unknown'
1630589888: 2: not freed: '0x7ff82bd9a808|s1' (1024 bytes) from 'unknown'
1630589888: 2: total-size count source
1630589888: 2: 0 0 Total of 0
1630589888: 2: ending time = 1630589888, elapsed since start = 0:00:02
- 添加了 dmalloc 引发的问题的链接。看这里