summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorWilliam D. Jones <thor0505@comcast.net>2022-02-16 10:24:40 -0500
committerPatrick <tingping@tingping.se>2022-02-16 12:44:09 -0600
commitccf6f431bbd96f04369875d72a61976bb6609a69 (patch)
tree400e48c602cdcf2299896706ecd6fe3243bb0372
parent73c0b672a26bb02885de87c898dea94404dc7199 (diff)
Return userdata from pluginprefs __pairs metamethod to avoid immediate GC.
-rw-r--r--plugins/lua/lua.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/plugins/lua/lua.c b/plugins/lua/lua.c
index d1370eaf..f8f051f4 100644
--- a/plugins/lua/lua.c
+++ b/plugins/lua/lua.c
@@ -957,16 +957,21 @@ static int api_hexchat_pluginprefs_meta_pairs(lua_State *L)
 	hexchat_plugin *h;
 
 	if(!script->name)
+
 		return luaL_error(L, "cannot use hexchat.pluginprefs before registering with hexchat.register");
 
 	dest = lua_newuserdata(L, 4096);
+
 	h = script->handle;
 	if(!hexchat_pluginpref_list(h, dest))
 		strcpy(dest, "");
 	lua_pushlightuserdata(L, dest);
 	lua_pushlightuserdata(L, dest);
 	lua_pushcclosure(L, api_hexchat_pluginprefs_meta_pairs_closure, 2);
-	return 1;
+	lua_insert(L, -2); // Return the userdata (second return value from pairs),
+						// even though it's not used by the closure (first return
+						// value from pairs), so that Lua knows not to GC it.
+	return 2;
 }
 
 static int api_attrs_meta_index(lua_State *L)
@@ -1764,4 +1769,3 @@ G_MODULE_EXPORT int hexchat_plugin_deinit(hexchat_plugin *plugin_handle)
 	g_clear_pointer(&expand_buffer, g_free);
 	return 1;
 }
-