lua plugin: cleanup properly when shutting down
authorRuben Kerkhof <ruben@rubenkerkhof.com>
Tue, 19 Feb 2019 09:57:03 +0000 (10:57 +0100)
committerRuben Kerkhof <ruben@rubenkerkhof.com>
Tue, 19 Feb 2019 09:58:46 +0000 (10:58 +0100)
Found while running collectd -T under valgrind:

==25971== 75 (64 direct, 11 indirect) bytes in 1 blocks are definitely lost in loss record 154 of 274
==25971==    at 0x483AB1A: calloc (vg_replace_malloc.c:762)
==25971==    by 0x4854009: lua_cb_register_read (lua.c:288)
==25971==    by 0x4FBE886: ???
==25971==    by 0x4FD4900: ???
==25971==    by 0x4FBEAD7: ???
==25971==    by 0x4FBEB04: ???
==25971==    by 0x4FBDF16: ???
==25971==    by 0x4FBEE5E: ???
==25971==    by 0x4FB53ED: ???
==25971==    by 0x48536E7: lua_script_load (lua.c:465)
==25971==    by 0x485387E: lua_config_script (lua.c:532)
==25971==    by 0x485395E: lua_config (lua.c:557)

src/lua.c

index d91676c..a9b981c 100644 (file)
--- a/src/lua.c
+++ b/src/lua.c
@@ -48,7 +48,7 @@ typedef struct lua_script_s {
 
 typedef struct {
   lua_State *lua_state;
-  const char *lua_function_name;
+  char *lua_function_name;
   pthread_mutex_t lock;
   int callback_id;
 } clua_callback_data_t;
@@ -263,6 +263,13 @@ static int lua_cb_dispatch_values(lua_State *L) /* {{{ */
   return 0;
 } /* }}} lua_cb_dispatch_values */
 
+static void lua_cb_free(void *data)
+{
+  clua_callback_data_t *cb = data;
+  free(cb->lua_function_name);
+  free(cb);
+}
+
 static int lua_cb_register_read(lua_State *L) /* {{{ */
 {
   int nargs = lua_gettop(L);
@@ -300,6 +307,7 @@ static int lua_cb_register_read(lua_State *L) /* {{{ */
                                             /* interval  = */ 0,
                                             &(user_data_t){
                                                 .data = cb,
+                                                .free_func = lua_cb_free,
                                             });
 
   if (status != 0)
@@ -342,6 +350,7 @@ static int lua_cb_register_write(lua_State *L) /* {{{ */
                                      /* callback  = */ clua_write,
                                      &(user_data_t){
                                          .data = cb,
+                                         .free_func = lua_cb_free,
                                      });
 
   if (status != 0)