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

时间:2015-06-17 14:47:10

标签: c memory-management valgrind

我正在努力克隆malloc (3)函数(mallocreallocfree

我想添加对Valgrind的支持。我正在使用these docs。但是,在添加对VALGRIND_MEMPOOL_FREEVALGRIND_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

这是我的realloc calling VALGRIND_MEMPOOL_FREEfree calling VALGRIND_MEMPOOL_FREE

这可能是什么原因?

2 个答案:

答案 0 :(得分:5)

  

这可能是什么原因?

这是由于valgrind中的一个错误。请在我对您的答案的评论中查看link到valgrind bug跟踪器。

我评论中的其他link

  

通过源代码粗略搜索表明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;
}

$ 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)
$ 

答案 1 :(得分:0)

从这里采取:http://valgrind.10908.n7.nabble.com/VALGRIND-MEMPOOL-FREE-not-reflected-in-heap-summary-td42789.html

  

通过源代码粗略搜索表明MEMPOOL_ALLOC   调用new_block,增加cmalloc_n_mallocs,但没有   对MEMPOOL_FREE中的cmalloc_n_frees的相应更改。这是一个   在MEMPOOL_FREE的最后增加它的补丁。这给了   我期待的行为。