From 7adbfa38c97c199056e8455803053bbf8e0470e8 Mon Sep 17 00:00:00 2001 From: Andrei Diaconu Date: Thu, 11 Jul 2013 11:03:15 +0200 Subject: [PATCH] Fix memory leaks for the case described in #3 (strings in array of structures) --- src/tpl.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/tpl.c b/src/tpl.c index e37b052..e4f9665 100644 --- a/src/tpl.c +++ b/src/tpl.c @@ -680,7 +680,7 @@ TPL_API void tpl_free(tpl_node *r) { int mmap_bits = (TPL_RDONLY|TPL_FILE); int ufree_bits = (TPL_MEM|TPL_UFREE); tpl_node *nxtc,*c; - int find_next_node=0,looking,i; + int find_next_node=0,looking,num,i; tpl_pidx *pidx,*pidx_nxt; /* For mmap'd files, or for 'ufree' memory images , do appropriate release */ @@ -707,12 +707,20 @@ TPL_API void tpl_free(tpl_node *r) { break; case TPL_TYPE_STR: /* free any packed (copied) string */ - for(i=0; i < c->num; i++) { - char *str = ((char**)c->data)[i]; - if (str) { - tpl_hook.free(str); - ((char**)c->data)[i] = NULL; - } + num = 1; + nxtc = c->next; + while (nxtc) { + if (nxtc->type == TPL_TYPE_POUND) { + num = nxtc->num; + } + nxtc = nxtc->next; + } + for (i = 0; i < c->num * num; i++) { + char *str = ((char**)c->data)[i]; + if (str) { + tpl_hook.free(str); + ((char**)c->data)[i] = NULL; + } } tpl_hook.free(c->data); find_next_node=1;