Repository navigation
ESP32: every engine global is reset at the start of each session, as on desktop - #5
Merged
Merged
Conversation
…on desktop Desktop's main.c calls OpenJKDF2_Globals_Reset() before each launch. tab5-emu runs the engine more than once per process and never did, so any global that assumes a fresh process carried over to the next launch; the handler table and the console and cheat counts were three cases already fixed by hand. jk_esp_engine_startup now calls the reset first. It runs on the engine thread after the previous session is torn down and before Main_Startup, the same order as desktop. The per-session arrays are already allocated by then; under PLATFORM_SESSION_ARRAYS the reset keeps their pointers and only zeroes their contents. - Replaces the hand resets in jk_esp_engine_startup (Window_ESP32_ResetMsgHandlers and five g_* writes), which the reset covers. - Also fixes a latent double free: jkHudInv_aItems was freed at shutdown but never cleared, so a later session that quit before loading a level freed it again. - Two leaks the reset would otherwise make permanent are fixed at the source: jkHudInv_Shutdown frees the HUD bitmaps of all SITHBIN_NUMBINS bins (it stopped at 100), and the loading-screen texts are all freed (the cleanup loop freed texts[0] twenty times) and freed at shutdown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
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.
Every engine global is reset at the start of each session, as on desktop
Desktop's
main.ccallsOpenJKDF2_Globals_Reset()before each launch. tab5-emu runs the engine several times per boot and never did, so any global that assumes a fresh process carried over to the next launch. #4 fixed three of these by hand: the handler table and the console and cheat counts.jk_esp_engine_startupnow calls the reset first. It runs on the engine thread after the previous session has been torn down and beforeMain_Startup, the same order as desktop. The session arrays (jk_esp_arrays_alloc) are already allocated by then; underPLATFORM_SESSION_ARRAYSthe reset keeps their pointers and only zeroes their contents.jk_esp_engine_startup(Window_ESP32_ResetMsgHandlersand fiveg_*writes). The source-level resets ofsithConsole_numRegisteredCmdsandjkDev_numCheatsstay: they fix the counts where they live, on every platform.jkHudInv_ShutdownfreesjkHudInv_aItemsbut never clears it. A later session that quit before loading a level freed it again, into JK's small-object pool, which doesn't detect double frees.jkHudInv_Shutdownfrees the HUD bitmaps of allSITHBIN_NUMBINSbins; it stopped at 100. Before, the next session freed bins 100–199 through pointers into the previous session's arena, which another game may have reused since.texts[0]twenty times and leaked the other 19 on every level load. They're now also freed at shutdown.Safety (audited before writing it)
NO_REINITscratch arrays and the heap message buffer (SITHCOMM_HEAP_MSGBUF).Main_Startup, as on a fresh boot.sithCogParse_ESP32_ReleaseLexer(called injk::deinit) having reset the lexer.Main_Startupstays broken: non-generated init flags such asjkRes_bInitstay set.Cost
11.7 KB of reset code in the jk overlay; the shared code window doesn't grow, since mgs is larger. About 3.5 KB of static PSRAM and 112 B of internal data that the link used to drop.
Testing (Tab5, with esp-cpp/tab5-emu#40)
files: 0 still openat each shutdown🤖 Generated with Claude Code
https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY