1

我们一直在使用dmalloc我们的工具集来验证我们的核心库是否没有内存泄漏。但是,最近我们发现使用printforfgetc会导致 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

更新

  1. 编译时也会出现这种情况g++
  2. 有人指出库 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
  1. 添加了 dmalloc 引发的问题的链接。看这里
4

1 回答 1

0

我将在这里回答我自己的问题。经过更多测试,我可以确认从其他库(例如 jemalloc 或 mtrace)中也观察到了这种行为。结论是,“libc”在不同平台上的实现肯定是不同的,在这种情况下,Linux 的实现(在我的 Debian 和 Windows 的 Ubuntu 上观察)在这些库可以的最后一点之前没有释放分配的缓冲区查看。

因此,这不是 dmalloc 中的错误。其他工具如“valgrind”将更适合这种情况。

于 2021-09-09T21:47:17.860 回答