Lua binding: Fix bug: Memory leak

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2018-07-25 15:16:18 +08:00
parent 34e94f6d5e
commit b8248347fa
3 changed files with 27 additions and 9 deletions

View File

@ -305,6 +305,10 @@ static void client_free(struct uh_client *cl)
kvlist_free(&cl->request.var);
kvlist_free(&cl->request.header);
cl->srv->nclients--;
if (cl->srv->on_client_free)
cl->srv->on_client_free(cl);
free(cl);
}
}

31
src/lua/uhttpd-lua.c 100755 → 100644
View File

@ -49,8 +49,10 @@ static void *uh_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg,
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, gc);
lua_setfield(L, -2, "__gc");
if (gc) {
lua_pushcfunction(L, gc);
lua_setfield(L, -2, "__gc");
}
luaL_setfuncs(L, reg, 0);
@ -285,11 +287,6 @@ static int lua_uh_get_body(lua_State *L)
return 1;
}
static int lua_uh_cli_free(lua_State *L)
{
return 0;
}
static const luaL_Reg client_reg[] = {
{"send_header", lua_uh_send_header},
{"append_header", lua_uh_append_header},
@ -307,7 +304,6 @@ static const luaL_Reg client_reg[] = {
{"get_query", lua_uh_get_query},
{"get_url", lua_uh_get_url},
{"get_body", lua_uh_get_body},
{ "free", lua_uh_cli_free },
{ NULL, NULL }
};
@ -319,9 +315,11 @@ static void lua_on_accept(struct uh_client *cl)
lua_pushlightuserdata(L, &cli_registry);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_pushlightuserdata(L, cl);
lcl = uh_create_userdata(L, sizeof(struct lua_uh_client), client_reg, LUA_UH_CLIENT_MT, lua_uh_cli_free);
lcl = uh_create_userdata(L, sizeof(struct lua_uh_client), client_reg, LUA_UH_CLIENT_MT, NULL);
lcl->cl = cl;
lua_rawset(L, -3);
lua_pop(L, 1);
}
static int lua_do_request_cb(lua_State *L, struct uh_client *cl)
@ -453,6 +451,20 @@ static const luaL_Reg server_reg[] = {
{ NULL, NULL }
};
static void lua_on_client_free(struct uh_client *cl)
{
lua_State *L = cl->srv->L;
lua_pushlightuserdata(L, &cli_registry);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_pushlightuserdata(L, cl);
lua_pushnil(L);
lua_rawset(L, -3);
lua_pop(L, 1);
}
static int lua_uh_new(lua_State *L)
{
int port = lua_tointeger(L, -1);
@ -473,6 +485,7 @@ static int lua_uh_new(lua_State *L)
lsrv->srv.L = L;
lsrv->srv.on_accept = lua_on_accept;
lsrv->srv.on_client_free = lua_on_client_free;
return 1;
}

View File

@ -37,6 +37,7 @@ struct uh_server {
void (*on_error404)(struct uh_client *cl);
int (*on_request)(struct uh_client *cl);
void (*on_accept)(struct uh_client *cl);
void (*on_client_free)(struct uh_client *cl);
#if (UHTTPD_SSL_SUPPORT)
int (*ssl_init)(struct uh_server *srv, const char *key, const char *crt);