[libcoap-developers] [PATCH] Memory leak from hash table
Status: Beta
Brought to you by:
obgm
|
From: Jean-Michel J. <Jea...@tr...> - 2016-04-05 16:26:58
|
Hi,
Using Valgrind under Linux , I discovered that there is some memory that is not released when the coap context is destroyed.
Here is a valgrind run with the example coapserver →
$ valgrind --tool=memcheck --leak-check=yes --show-leak-kinds=all ./coapserver2
==22077== Memcheck, a memory error detector
==22077== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==22077== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==22077== Command: ./coapserver2
==22077==
^C==22077==
==22077== HEAP SUMMARY:
==22077== in use at exit: 576 bytes in 2 blocks
==22077== total heap usage: 16 allocs, 14 frees, 1,671 bytes allocated
==22077==
==22077== 512 bytes in 1 blocks are indirectly lost in loss record 1 of 2
==22077== at 0x4C2DB95: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22077== by 0x4090BF: coap_add_resource (resource.c:400)
==22077== by 0x403915: init_resources (coap-server.c:297)
==22077== by 0x4040FD: main (coap-server.c:495)
==22077==
==22077== 576 (64 direct, 512 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==22077== at 0x4C2DB95: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22077== by 0x409084: coap_add_resource (resource.c:400)
==22077== by 0x403915: init_resources (coap-server.c:297)
==22077== by 0x4040FD: main (coap-server.c:495)
==22077==
==22077== LEAK SUMMARY:
==22077== definitely lost: 64 bytes in 1 blocks
==22077== indirectly lost: 512 bytes in 1 blocks
==22077== possibly lost: 0 bytes in 0 blocks
==22077== still reachable: 0 bytes in 0 blocks
==22077== suppressed: 0 bytes in 0 blocks
==22077==
==22077== For counts of detected and suppressed errors, rerun with: -v
==22077== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
I tracked it in the hash table for the resources, here is my fix →
src/resource.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/resource.c b/src/resource.c
index 0aa1283..61ab87c 100644
--- a/src/resource.c
+++ b/src/resource.c
@@ -457,6 +457,7 @@ coap_delete_all_resources(coap_context_t *context) {
LL_FOREACH_SAFE(context->resources, res, rtmp) {
#else
HASH_ITER(hh, context->resources, res, rtmp) {
+ HASH_DELETE(hh, context->resources, res);
#endif
coap_free_resource(res);
}
This is the Valgrind output after the fix →
$ valgrind --tool=memcheck --leak-check=yes --show-leak-kinds=all ./coapserver2
==22886== Memcheck, a memory error detector
==22886== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==22886== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==22886== Command: ./coapserver2
==22886==
^C==22886==
==22886== HEAP SUMMARY:
==22886== in use at exit: 0 bytes in 0 blocks
==22886== total heap usage: 16 allocs, 16 frees, 1,671 bytes allocated
==22886==
==22886== All heap blocks were freed -- no leaks are possible
==22886==
==22886== For counts of detected and suppressed errors, rerun with: -v
==22886== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Thanks
Jean-Michel Julien
|