Skip to content

ESP32: every engine global is reset at the start of each session, as on desktop - #5

Merged
finger563 merged 1 commit into
esp32p4from
esp32/session-globals-reset
Nov 23, 2026
Merged

finger563 merged 1 commit into
esp32p4from
esp32/session-globals-reset

Conversation

@finger563

@finger563 finger563 commented Nov 23, 2026 •

Copy link
Copy Markdown

Every engine global is reset at the start of each session, as on desktop

Desktop's main.c calls OpenJKDF2_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_startup now calls the reset first. It runs on the engine thread after the previous session has been torn down and before Main_Startup, the same order as desktop. The session arrays (jk_esp_arrays_alloc) 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). The source-level resets of sithConsole_numRegisteredCmds and jkDev_numCheats stay: they fix the counts where they live, on every platform.
  • Fixes a latent double free. jkHudInv_Shutdown frees jkHudInv_aItems but 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.
  • 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. Before, the next session freed bins 100–199 through pointers into the previous session's arena, which another game may have reused since.
    • The loading-screen texts are all freed. The cleanup loop freed 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)

  • A script compared every generated global's reset value with its boot initializer and found no differences. The reset skips the NO_REINIT scratch arrays and the heap message buffer (SITHCOMM_HEAP_MSGBUF).
  • The port writes only the five session-array pointers before the call, and the reset keeps them. jk.cpp, jk_music.cpp and main/ reference none of the 851 generated names.
  • Settings and the player name come back from the registry and the profile during Main_Startup, as on a fresh boot.
  • The first COG parse of a session relies on sithCogParse_ESP32_ReleaseLexer (called in jk::deinit) having reset the lexer.
  • Not covered: about 660 file-scope and function statics, which the reset doesn't touch on desktop either. A retry after a failed Main_Startup stays broken: non-generated init flags such as jkRes_bInit stay set.
  • The plan was verified by two skeptics, who found no blocking problems.

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)

Result
Four sessions in one boot: level 1 → 2 and save; quit during the intro; load that save from the intro and continue to level 4; level 1 → 2 again all pass; every tally takes Enter; no "not recognized"; files: 0 still open at each shutdown
The whole light campaign 01 → 18 in a third session; 15maw → the dark ending and 18Ascend → the light ending in later sessions every level loads; memory as in first sessions (largest block 9.2 MB)
Launcher PSRAM after each quit equal to the boot value (18,830 K) in every session; internal RAM drops 216 B once after the first session, then stays flat
Material cache count at level 1 the same in every session, so nothing stale is left in the LRU

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant