From 8c2bd1e3b3df6a03be3311a0ff57a3e819e83fc8 Mon Sep 17 00:00:00 2001 From: Rui Guo Date: Mon, 15 Jun 2009 21:10:08 +0800 Subject: [PATCH] Fix an memory leak on unhook ticket. --- src/lua.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lua.c b/src/lua.c index 1be2b69..332c599 100644 --- a/src/lua.c +++ b/src/lua.c @@ -356,7 +356,20 @@ static const luaL_reg callback_methods[] = { {0, 0} }; +int +callback_gc(lua_State *L) +{ + lua_handler lh = check_callback(L, 1); + /* If the callback is not unhooked, we can not reclaim this memory since + * it's still used by the event dispatcher */ + if (lh->listener) + LuaFreeHandler(L, &lh); + + return 0; +} + static const luaL_reg callback_metamethods[] = { + {"__gc", callback_gc}, {0, 0} }; @@ -898,6 +911,7 @@ int LuaSource(const char *file, int async) if (luaL_dofile(L, file) && lua_isstring(L, -1)) { LuaShowErr(L); + return 0; } return 1; } @@ -1241,8 +1255,6 @@ LuaRegEvent(lua_State *L) return luaL_error(L, "Handler has already been registered"); } /* Return the handler for un-register */ - /*l->handler = lua_newuserdata(L, sizeof(struct lua_handler)); - memcpy(l->handler, &lh, sizeof(struct lua_handler));*/ lh->listener = l; push_callback(L, (lua_handler *)&l->handler); } -- 2.11.4.GIT