From ea0c9c18c96edceb626bbc2f21e7b4917780ac57 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Sun, 22 Nov 2026 20:53:17 -0700 Subject: [PATCH] ESP32: 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 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 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- src/Gui/jkGUITitle.c | 13 ++++++++++--- src/Main/jkHudInv.c | 2 +- src/Platform/ESP32/Window_ESP32.c | 13 ++++++------- src/Win95/Window.c | 19 ------------------- src/Win95/Window.h | 3 --- 5 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/Gui/jkGUITitle.c b/src/Gui/jkGUITitle.c index bd2cbb7f2..171a80376 100644 --- a/src/Gui/jkGUITitle.c +++ b/src/Gui/jkGUITitle.c @@ -105,6 +105,13 @@ void jkGuiTitle_Shutdown() // Added: clean reset memset(jkGuiTitle_versionBuffer, 0, sizeof(jkGuiTitle_versionBuffer)); jkGuiTitle_loadPercent = 0; + for (int i = 0; i < 20; i++) // Added: the last level's loading-screen texts + { + if (jkGuiTitle_aTexts[i].str) { + STD_FREE(jkGuiTitle_aTexts[i].str); + jkGuiTitle_aTexts[i].str = NULL; + } + } } char jkGuiTitle_sub_4189A0(char *a1) @@ -145,9 +152,9 @@ char16_t* jkGuiTitle_quicksave_related_func1(stdStrTable *strTable, char *jkl_fn // Added: cleanup for (int i = 0; i < 20; i++) { - if (texts->str) { - STD_FREE(texts->str); - texts->str = NULL; + if (jkGuiTitle_aTexts[i].str) { // Added: each text (was texts[0] twenty times) + STD_FREE(jkGuiTitle_aTexts[i].str); + jkGuiTitle_aTexts[i].str = NULL; } } _memset(jkGuiTitle_aTexts, 0, sizeof(jkGuiTitle_aTexts)); diff --git a/src/Main/jkHudInv.c b/src/Main/jkHudInv.c index b3a014be3..095524ac2 100644 --- a/src/Main/jkHudInv.c +++ b/src/Main/jkHudInv.c @@ -1007,7 +1007,7 @@ int jkHudInv_Shutdown() jkHudInv_aBitmaps[2] = 0; } - for (int i = 0; i < 100; i++) + for (int i = 0; i < SITHBIN_NUMBINS; i++) // Added: all the bins jkHudInv_LoadItemRes loads (was 100) { if ( (sithInventory_g_aTypes[i].flags & 0xA) != 0 && sithInventory_g_aTypes[i].hudBitmap ) { diff --git a/src/Platform/ESP32/Window_ESP32.c b/src/Platform/ESP32/Window_ESP32.c index d58e5ded5..ed0942d98 100644 --- a/src/Platform/ESP32/Window_ESP32.c +++ b/src/Platform/ESP32/Window_ESP32.c @@ -95,17 +95,16 @@ void Window_Main_Loop() int jk_esp_engine_startup(const char* game_dir) { + // Every engine global back to its boot value, as desktop's main.c does + // before each launch: tab5-emu runs the engine more than once per process. + // The session arrays (jk_esp_arrays_alloc, already made) keep their pointers + // and only have their contents zeroed (PLATFORM_SESSION_ARRAYS). + OpenJKDF2_Globals_Reset(); + char cmdLine[256]; strncpy(cmdLine, jk_esp_engine_args(), sizeof(cmdLine) - 1); cmdLine[sizeof(cmdLine) - 1] = 0; - Window_ESP32_ResetMsgHandlers(); // the table too, not only the count (see Window.c) - g_thing_two_some_dialog_count = 0; - g_should_exit = 0; - g_window_not_destroyed = 0; - g_hInstance = 0; - g_nShowCmd = 0; - // the engine opens everything relative to the working directory if (game_dir && game_dir[0]) { chdir(game_dir); diff --git a/src/Win95/Window.c b/src/Win95/Window.c index 41cb86c3f..ee015955b 100644 --- a/src/Win95/Window.c +++ b/src/Win95/Window.c @@ -151,25 +151,6 @@ int Window_RemoveMsgHandler(WindowHandler_t a1) return 1; } -#ifdef TARGET_ESP32 -// Added: tab5-emu runs the engine more than once in a process. The handler -// table outlived a session (stdControl's handler is never removed, and -// jkGuiRend_Shutdown forgets the GUI's), but only the count was reset. The next -// session's adds were refused as duplicates, so dispatch, which visits -// g_handler_count slots, stopped before the GUI's handler. Keys and taps were -// ignored at the menus (only the on-screen pad still worked: jkGuiRend_UpdateController -// calls the GUI's handler directly), and Esc no longer opened the in-game menu. -void Window_ESP32_ResetMsgHandlers(void) -{ - for (int i = 0; i < 16; i++) - { - Window_ext_handlers[i].handler = 0; - Window_ext_handlers[i].exists = 0; - } - g_handler_count = 0; -} -#endif - int Window_AddDialogHwnd(HWND a1) { int v1; // eax diff --git a/src/Win95/Window.h b/src/Win95/Window.h index ce62bf9c1..cd2fec387 100644 --- a/src/Win95/Window.h +++ b/src/Win95/Window.h @@ -23,9 +23,6 @@ extern int Window_ySize; int Window_AddMsgHandler(WindowHandler_t a1); int Window_RemoveMsgHandler(WindowHandler_t a1); -#ifdef TARGET_ESP32 -void Window_ESP32_ResetMsgHandlers(void); -#endif int Window_AddDialogHwnd(HWND a1); int Window_msg_main_handler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); int Window_Main(HINSTANCE hInstance, int a2, char *lpCmdLine, int nShowCmd, LPCSTR lpWindowName);