自定义分配器:Valgrind 显示 7 次分配,0 次释放,无泄漏
Custom allocator: Valgrind shows 7 allocs, 0 frees, no leaks
我正在研究 malloc (3)
函数的克隆(目前为 malloc
、realloc
和 free
)。
我想添加对 Valgrind 的支持。我正在使用 these docs。但是,在添加对 VALGRIND_MEMPOOL_FREE
、VALGRIND_MEMPOOL_ALLOC
和 VALGRIND_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_FREE
and my free
calling VALGRIND_MEMPOOL_FREE
.
这可能是什么原因?
取自此处:http://valgrind.10908.n7.nabble.com/VALGRIND-MEMPOOL-FREE-not-reflected-in-heap-summary-td42789.html
A cursory search through the source code indicates that MEMPOOL_ALLOC
calls new_block, which increments cmalloc_n_mallocs, but there is no
corresponding change to cmalloc_n_frees in MEMPOOL_FREE. Here's a
patch that increments it at the very end of MEMPOOL_FREE. This gives
me the behavior I expect.
What could be the cause of this ?
这是由于 valgrind 中的错误。请参阅我对您的回答的评论中的 valgrind 错误跟踪器 link。
来自其他 link 我的评论:
A cursory search through the source code indicates that MEMPOOL_ALLOC
calls new_block, which increments cmalloc_n_mallocs, but there is no
corresponding change to cmalloc_n_frees in MEMPOOL_FREE.
/* 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)
$
我正在研究 malloc (3)
函数的克隆(目前为 malloc
、realloc
和 free
)。
我想添加对 Valgrind 的支持。我正在使用 these docs。但是,在添加对 VALGRIND_MEMPOOL_FREE
、VALGRIND_MEMPOOL_ALLOC
和 VALGRIND_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_FREE
and my free
calling VALGRIND_MEMPOOL_FREE
.
这可能是什么原因?
取自此处:http://valgrind.10908.n7.nabble.com/VALGRIND-MEMPOOL-FREE-not-reflected-in-heap-summary-td42789.html
A cursory search through the source code indicates that MEMPOOL_ALLOC calls new_block, which increments cmalloc_n_mallocs, but there is no corresponding change to cmalloc_n_frees in MEMPOOL_FREE. Here's a patch that increments it at the very end of MEMPOOL_FREE. This gives me the behavior I expect.
What could be the cause of this ?
这是由于 valgrind 中的错误。请参阅我对您的回答的评论中的 valgrind 错误跟踪器 link。
来自其他 link 我的评论:
A cursory search through the source code indicates that MEMPOOL_ALLOC calls new_block, which increments cmalloc_n_mallocs, but there is no corresponding change to cmalloc_n_frees in MEMPOOL_FREE.
/* 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)
$