From fdb363df4b71f7b6bfbc7d3e1c597849b99f765d Mon Sep 17 00:00:00 2001 From: bjorn Date: Tue, 17 Jan 2017 22:11:17 -0800 Subject: [PATCH] Get closure on that lovr.event memory leak; --- src/lovr/event.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lovr/event.c b/src/lovr/event.c index a5371020..be03fcf6 100644 --- a/src/lovr/event.c +++ b/src/lovr/event.c @@ -2,6 +2,8 @@ #include "event/event.h" #include "util.h" +static int pollRef; + static int nextEvent(lua_State* L) { Event* event = lovrEventPoll(); @@ -44,6 +46,11 @@ const luaL_Reg lovrEvent[] = { int l_lovrEventInit(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrEvent); + + // Store nextEvent in the registry to avoid creating a closure every time we poll for events. + lua_pushcfunction(L, nextEvent); + pollRef = luaL_ref(L, LUA_REGISTRYINDEX); + map_init(&EventTypes); map_set(&EventTypes, "quit", EVENT_QUIT); lovrEventInit(); @@ -56,7 +63,7 @@ int l_lovrEventClear(lua_State* L) { } int l_lovrEventPoll(lua_State* L) { - lua_pushcclosure(L, nextEvent, 0); + lua_rawgeti(L, LUA_REGISTRYINDEX, pollRef); return 1; }