From 5314144e7705ff205a81964188bf8200791462e5 Mon Sep 17 00:00:00 2001 From: PeachyPeach <72323920+PeachyPeachSM64@users.noreply.github.com> Date: Sat, 4 Jan 2025 00:50:35 +0100 Subject: [PATCH] Fix smlua memory leaks (#606) --- mods/arena/arena-sound.lua | 2 -- src/pc/lua/smlua.c | 16 +++++++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/mods/arena/arena-sound.lua b/mods/arena/arena-sound.lua index 30f026087..1973565ac 100644 --- a/mods/arena/arena-sound.lua +++ b/mods/arena/arena-sound.lua @@ -34,7 +34,6 @@ function handleMusic() audioCurSeq = get_current_background_music() if audioMain then audio_stream_stop(audioMain) - audio_stream_destroy(audioMain) audioMain = nil end if bgms[curMap] and bgms[curMap].audio then @@ -76,7 +75,6 @@ function handleMusic() else if audioSpecial then audio_stream_stop(audioSpecial) - audio_stream_destroy(audioSpecial) audioSpecial = nil if audioMain and audioMainPaused then audioMainPaused = false diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index 75432f9cc..acf20e70c 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -344,19 +344,17 @@ void smlua_update(void) { audio_sample_destroy_pending_copies(); smlua_call_event_hooks(HOOK_UPDATE); + // Collect our garbage after calling our hooks. // If we don't, Lag can quickly build up from our mods. - /*lua_gc(L, LUA_GCSTEP, 1); - lua_gc(L, LUA_GCSTOP, 0);*/ - - // EDIT: uhh, it turns out that is not the case - // if we only do incremental garbage collection, - // eventually the garbage will build up so much - // that the game slows to a crawl. Messing with - // the GC setting is what caused Arena to get worse - // over time. + // Truth is smlua generates so much garbage that the + // incremental collection fails to keep up after some time. + // So, for now, stop the GC from running during the hooks + // and perform a full GC at the end of the frame. // The real fix would be to make smlua produce less // garbage. + lua_gc(L, LUA_GCSTOP, 0); + lua_gc(L, LUA_GCCOLLECT, 0); } void smlua_shutdown(void) {