Fix a Lua state leak on script termination.

This commit is contained in:
Alexey Kopytov
2017-01-30 08:48:26 +03:00
parent ba312d33fb
commit 6eb5ede654
3 changed files with 33 additions and 29 deletions

View File

@ -57,6 +57,8 @@
#define REPORT_INTERMEDIATE_HOOK "report_intermediate"
#define REPORT_CUMULATIVE_HOOK "report_cumulative"
#define xfree(ptr) ({ if ((ptr) != NULL) free((void *) ptr); ptr = NULL; })
/* Interpreter context */
typedef struct {
@ -204,12 +206,6 @@ static bool func_available(lua_State *L, const char *func)
return rc;
}
static void xfree(const void *ptr)
{
if (ptr != NULL)
free((void *) ptr);
}
/* Export command line options */
static int do_export_options(lua_State *L, bool global)
@ -376,15 +372,36 @@ sb_test_t *sb_load_lua(const char *testname)
error:
sb_lua_close_state(gstate);
xfree(states);
xfree(sbtest.sname);
xfree(sbtest.lname);
sb_lua_done();
return NULL;
}
void sb_lua_done(void)
{
sb_lua_close_state(gstate);
gstate = NULL;
xfree(states);
if (sbtest.args != NULL)
{
for (size_t i = 0; sbtest.args[i].name != NULL; i++)
{
xfree(sbtest.args[i].name);
xfree(sbtest.args[i].desc);
xfree(sbtest.args[i].value);
}
xfree(sbtest.args);
}
xfree(sbtest.sname);
xfree(sbtest.lname);
}
/* Initialize Lua script */
int sb_lua_op_init(void)
@ -477,8 +494,6 @@ int sb_lua_op_thread_done(int thread_id)
int sb_lua_op_done(void)
{
xfree(states);
lua_getglobal(gstate, DONE_FUNC);
if (!lua_isnil(gstate, -1))
{
@ -489,22 +504,7 @@ int sb_lua_op_done(void)
}
}
sb_lua_close_state(gstate);
if (sbtest.args != NULL)
{
for (size_t i = 0; sbtest.args[i].name != NULL; i++)
{
xfree(sbtest.args[i].name);
xfree(sbtest.args[i].desc);
xfree(sbtest.args[i].value);
}
free(sbtest.args);
}
xfree(sbtest.sname);
xfree(sbtest.lname);
sb_lua_done();
return 0;
}

View File

@ -27,6 +27,8 @@
sb_test_t *sb_load_lua(const char *testname);
void sb_lua_done(void);
int sb_lua_hook_call(lua_State *L, const char *name);
bool sb_lua_custom_command_defined(const char *name);

View File

@ -1513,6 +1513,8 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}
sb_lua_done();
db_done();
sb_counters_done();