Skip to content

Commit 990d0b7

Browse files
committed
run Lua garbage collector on mission start
This might fix #2961. By explicitly running the garbage collector before a mission starts, this will ensure each mission begins with a clean slate.
1 parent 7c5371a commit 990d0b7

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

freespace2/freespace.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@
188188
#include "weapon/shockwave.h"
189189
#include "weapon/weapon.h"
190190

191+
// for the Lua garbage collector
192+
extern "C"
193+
{
194+
#include "lgc.h"
195+
}
196+
197+
191198
#include <SDL.h>
192199
#include <SDL_main.h>
193200

@@ -1360,7 +1367,13 @@ bool game_start_mission()
13601367

13611368
game_busy( NOX("** starting mission_load() **") );
13621369
load_mission_load = (uint) time(nullptr);
1363-
if ( !mission_load(Game_current_mission_filename) ) {
1370+
bool load_success = mission_load(Game_current_mission_filename);
1371+
load_mission_load = (uint)(time(nullptr) - load_mission_load);
1372+
1373+
// free up memory from parsing the mission
1374+
stop_parse();
1375+
1376+
if ( !load_success ) {
13641377
if ( !(Game_mode & GM_MULTIPLAYER) ) {
13651378
popup(PF_BODY_BIG | PF_USE_AFFIRMATIVE_ICON, 1, POPUP_OK, XSTR( "Attempt to load the mission failed", 169));
13661379
gameseq_post_event(GS_EVENT_MAIN_MENU);
@@ -1376,11 +1389,15 @@ bool game_start_mission()
13761389

13771390
return false;
13781391
}
1379-
load_mission_load = (uint) (time(nullptr) - load_mission_load);
13801392

1381-
// free up memory from parsing the mission
1382-
extern void stop_parse();
1383-
stop_parse();
1393+
// Since we just freed up memory, now is probably a good time to run the Lua garbage collector.
1394+
// This is also after the preliminary checks are complete but before things start getting paged in.
1395+
auto L = Script_system.GetLuaSession();
1396+
if (L != nullptr)
1397+
{
1398+
game_busy(NOX("** cleaning up Lua objects **"));
1399+
luaC_fullgc(L);
1400+
}
13841401

13851402
game_busy( NOX("** starting game_post_level_init() **") );
13861403
load_post_level_init = (uint) time(nullptr);

0 commit comments

Comments
 (0)