自定义分配器:Valgrind显示7个分配,0个释放,没有泄漏

con*_*com 6 c valgrind memory-management

我工作的一个克隆malloc (3)功能(malloc,reallocfree现在).

我想添加对Valgrind的支持.我正在使用这些文档.然而,增加呼叫后VALGRIND_MEMPOOL_FREE,VALGRIND_MEMPOOL_ALLOCVALGRIND_CREATE_MEMPOOL宏,我从Valgrind的以下内容:

==22303== HEAP SUMMARY:
==22303==     in use at exit: 0 bytes in 0 blocks
==22303==   total heap usage: 7 allocs, 0 frees, 2,039 bytes allocated
==22303== 
==22303== All heap blocks were freed -- no leaks are possible
Run Code Online (Sandbox Code Playgroud)

这是我的realloc呼唤VALGRIND_MEMPOOL_FREE和我的free呼唤VALGRIND_MEMPOOL_FREE.

可能是什么原因造成的?

456*_*976 5

可能是什么原因造成的?

这是由于valgrind中的一个错误.在我对你的答案的评论中查看valgrind bug跟踪器的链接.

从我评论中的其他链接:

粗略搜索源代码表示MEMPOOL_ALLOC调用new_block,它会增加cmalloc_n_mallocs,但MEMPOOL_FREE中的cmalloc_n_frees没有相应的更改.

/* valgrind.c */
#include <valgrind/valgrind.h>

int main(int argc, char *argv[]) {
    char pool[100];

    VALGRIND_CREATE_MEMPOOL(pool, 0, 0);
    VALGRIND_MEMPOOL_ALLOC(pool, pool, 8);
    VALGRIND_MEMPOOL_FREE(pool, pool);
    VALGRIND_DESTROY_MEMPOOL(pool);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

$ gcc valgrind.c -g
$ valgrind --leak-check=full --show-leak-kinds=all ./a.out
==10186== Memcheck, a memory error detector
==10186== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==10186== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==10186== Command: ./a.out
==10186== 
==10186== 
==10186== HEAP SUMMARY:
==10186==     in use at exit: 0 bytes in 0 blocks
==10186==   total heap usage: 1 allocs, 0 frees, 8 bytes allocated
==10186== 
==10186== All heap blocks were freed -- no leaks are possible
==10186== 
==10186== For counts of detected and suppressed errors, rerun with: -v
==10186== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$ 
Run Code Online (Sandbox Code Playgroud)