Repository navigation
ESP32: loading a save before the title screen no longer crashes; once-per-process buffers freed with the engine - #2
Merged
Conversation
…pause-menu load closes the engine menus A save loaded from the tab5-emu pause menu while the intro was still playing crashed in sithKeyFrame_GetByIdx (load fault at +0x20, lw a3,364(a4) with a4 = sithWorld_g_pStaticWorld = NULL; core dump: a4=0, a3=0xffff7fff, a5=0x22, i.e. static keyframe 0x8022 on the first track of the first DSS_PUPPETSTATUS). The static world (static.jkl) and the inventory bins (items.dat) are loaded by jkMain_TitleShow, which runs after the intro and before any menu can start a level; the pause menu's load switches VIDEO -> UNK -> GAMEPLAY directly, so the level and the save's messages were processed without them. Loading in a session that already had a level (or that had shown the title) never hit this. - jkMain_GameplayShow: if no static world is open yet, open static.jkl and load items.dat, as jkMain_TitleShow and Main_StartupDedicated's autostart do. A no-op on every path that went through the title. - jk_esp_engine_load: before the title the Players dialog has not read a profile either (controls have no bindings): read the remembered player's profile, or fall back to the default controls. - jk_esp_engine_load at an engine menu (main menu, Players dialog, Single Player, Setup, the in-game Esc menu): the level switch only happens once no modal menu is open, and on RETRO targets a menu does not close by itself for jkGuiRend_thing_four, so the load waited for the user to leave the menus (and New Game replaced it). jkGuiRend_ESP32_bCloseMenus makes every menu return -1 and jkGuiMain_Show leave its loop until jkMain_GuiAdvance performs the switch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
… / new player screens; static.jkl before the episode GOB; quit at the main menu returns Review follow-ups to the fresh-load fix (0ac4f63): - jkGuiRend_ESP32_bCloseMenus made every menu return -1, but the Force and single-player tally screens answer -1 with an "abort?" Yes/No (which then also returned -1, i.e. No) and reopened, and the new player dialog reopens on -1 while no profile exists: a load at Esc > Jedi Powers, the end-of-level tally or star screen, the dark/light choice or a first-run Players dialog spun forever and Quit could not end it. Those screens now return while the flag is set (the new player dialog with the default controls, since no profile was read). - jkMain_EndLevelScreenShow, jkMain_ChoiceShow and the cutscenes menu fall back to the main menu on -1, which replaced the pending load (the cart reported success): skipped while the flag is set. - jkMain_VideoLeave starts the next level when an inter-level cutscene (VIDEO3 / VIDEO4) ends, and jkMain_GuiAdvance takes that state over the one it is switching to: a load during one was replaced by the next level. Skipped when switching to JK_GAMEMODE_UNK (only ever a load). - static.jkl and items.dat for a level reached without the title are now opened in jkMain_UnkShow, with the title's loading screen, before jkMain_UnkTick mounts the save's episode GOB: jkRes searches the episode directory first, so in jkMain_GameplayShow the session's static world could pick up files from that episode. GameplayShow keeps the guard as a fallback. - The flag is reset in jkGuiRend_Shutdown, so it cannot reach the next launch. - A quit from outside the engine (Window_MessageLoop) also sets it: only cancelling the active menu left jkGuiMain_Show reopening the main menu, so a quit at JK's main menu never returned and jk::deinit waited forever. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
…h the engine Both are made once per process: the lexer's 16 KB input buffer by the first COG parse (during the first level load) and the software mixer's chunk buffer by the first stdSound_Startup. In tab5-emu the engine runs in a process that outlives it, so they stayed in the middle of the PSRAM heap after the game: the first Jedi Knight session split the largest free block from 18.4 MB to 9.5-12.5 MB for the rest of the boot. - sithCogParse_ESP32_ReleaseLexer() deletes the lexer's buffer and resets yy_init; yyrestart() makes a new buffer when there is none, as newer flex does (this one passed the NULL buffer on). - stdSound_ESP32_Release() frees the mix buffer and clears stdSound_bInitted so the next startup makes a new one. The port calls both once the engine and its audio task have stopped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
This was referenced Nov 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #1 (esp32/alloc-arrays-at-start).
Loading a save before the title screen crashed
A save loaded from tab5-emu's pause menu while JK's intro was still playing crashed in
sithKeyFrame_GetByIdx, fromsithGamesave_Restore→RestoreFile→sithDSS_ProcessPuppetStatus. The static world,sithWorld_g_pStaticWorld, was NULL: the load switches VIDEO → UNK → GAMEPLAY and so skipsjkMain_TitleShow. That function is the only one that opensstatic.jkl(the player's models, puppets and keyframes) and loadsitems.dat, so the save's first static keyframe (0x8022) was read through NULL. A relaunch behaved like a fresh boot.jkMain_UnkShownow opensstatic.jklanditems.datwhen the title hasn't run. It does this behind the title's loading screen, before the save's episode GOB is mounted, so the episode can't override static resources.jkMain_GameplayShowkeeps the same check as a fallback.jk_esp_save.c), or falls back to the default controls. Before the title, the Players dialog hasn't bound any controls.jkGuiRend_ESP32_bCloseMenus). Before, nothing happened until the user left the menus.jkGuiRend_Shutdown.Buffers made once per process are freed with the engine
The COG lexer's 16 KB input buffer, made by the first level load, and the software mixer's chunk buffer were kept for the whole process. In tab5-emu that left them in the middle of the PSRAM heap after the game, so the first JK session split the largest free block from 18.4 MB down to 9.5–12.5 MB for the rest of the boot.
sithCogParse_ESP32_ReleaseLexer()frees the lexer's buffer and resetsyy_init.yyrestart()now makes a new buffer when there is none, as newer flex does.stdSound_ESP32_Release()frees the mix buffer.Testing (Tab5, with esp-cpp/tab5-emu#35)
The fix was reviewed adversarially before testing. The review caught a hang on the Force and tally screens and a too-late
static.jklload in the first version, both fixed.🤖 Generated with Claude Code
https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY