From f54ac3d55f9acc3786369509df1bdff737e6d444 Mon Sep 17 00:00:00 2001 From: Nicolas CARRIER Date: Fri, 13 Apr 2012 13:51:44 +0200 Subject: [PATCH] Memory leak in classid.c I'm using libnl in a program which I give to valgrind in order to track memory errors / leaks. When my program exits, it complains about non-freed memory, allocated in 3 places in classid.c, at lines 280, 284 and 289. It seems related to the module's constructor classid_init which allocates resources, with no destructor to free it. The attached patch tries to fix this issue by registering a destructor which performs the tree liberation at exit. --- lib/route/classid.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/route/classid.c b/lib/route/classid.c index abed244..da531bd 100644 --- a/lib/route/classid.c +++ b/lib/route/classid.c @@ -41,7 +41,7 @@ static int compare_id(const void *pa, const void *pb) if (ma->classid < mb->classid) return -1; - + if (ma->classid > mb->classid) return 1; @@ -217,7 +217,7 @@ not_a_number: } else { /* XXXX:YYYY */ uint32_t l; - + update: l = strtoul(colon+1, &end, 16); @@ -296,7 +296,7 @@ static int classid_map_add(uint32_t classid, const char *name) /** * (Re-)read classid file - * + * * Rereads the contents of the classid file (typically found at the location * /etc/libnl/classid) and refreshes the classid maps. * @@ -409,7 +409,7 @@ int rtnl_classid_generate(const char *name, uint32_t *result, uint32_t parent) fclose(fd); if ((err = classid_map_add(classid, name)) < 0) { - /* + /* * Error adding classid map, re-read classid file is best * option here. It is likely to fail as well but better * than nothing, entry was added to the file already anyway. @@ -438,4 +438,12 @@ static void __init classid_init(void) fprintf(stderr, "Failed to read classid file: %s\n", nl_geterror(err)); } +static void __exit classid_exit(void) +{ + void free_map(void *map) { + free(((struct classid_map *)map)->name); + free(map); + }; + tdestroy(id_root, free_map); +} /** @} */