Skip to content

ESP32: checking that a cutscene exists no longer leaks the whole video file - #3

Merged
finger563 merged 1 commit into
esp32p4from
esp32/cutscene-check-leak
Nov 23, 2026
Merged

finger563 merged 1 commit into
esp32p4from
esp32/cutscene-check-leak

Conversation

@finger563

@finger563 finger563 commented Nov 23, 2026 •

Copy link
Copy Markdown

Checking that a cutscene exists leaked the whole video file

Before each cutscene, jkMain_cd_swap_reverify calls util_FileExists("video\X.smk"), which goes through jkRes_FileOpen. That function walks the GOB directories in order:

  1. At episode\JK1 the low-level open fails, so it calls stdGob_FileOpen(JK1.GOB, …).
  2. With QOL_IMPROVEMENTS, that function first calls stdEmbeddedRes_LoadOnlyInternal. It found the loose resource/video/X.smk and read the whole file into a malloc as a memory-mapped GOB handle.
  3. The walk didn't stop there. At resource the low-level open also succeeds, and it replaced the slot's handle without closing the first one.

So the copy was never freed: 7.1 MB for 23A.SMK after level 8, 3.3 MB for 25A after level 9, and so on for every cutscene small enough to fit. The 18–56 MB ones just failed the malloc. JK's allocator doesn't track that memory, so it also survived the engine's shutdown.

In tab5-emu this was why 09FuelStation failed to load in a continuous run (largest free block 2 MB at the georesource section), and why quitting JK at a tally, a cutscene or a level load lost 7–10 MB of PSRAM until reboot. Found with a heap hook that records the call stack of every allocation of 1 MB or more: jkMain_cd_swap_reverify → util_FileExists → jkRes_FileOpen → stdGob_FileOpen → stdEmbeddedRes_LoadOnlyInternal.

Changes

  • jkRes_FileOpen: a later directory's loose file still replaces an earlier hit, but the earlier handle (a FILE or a GOB handle) is now closed first. "Stop at the first hit" would break cutscenes: jkRes_FileExists hands smk_open_file the slot's path, and only the low-level hit has the real resource\video\… one.
  • stdGob_FileOpen: the loaded copy is freed on the two returns where no GOB handle is free.
  • stdEmbeddedRes: a short read frees the copy and closes the file. Found by the review.
  • ESP32 only: stdEmbeddedRes no longer reads loose files over 1 MB whole. jkRes opens them from resource/ as streams, so a cutscene check no longer reads the whole video from the SD card twice.

Upstream OpenJKDF2 has the same leak on desktop; it goes unnoticed there.

Testing (Tab5, with esp-cpp/tab5-emu#38)

Both full JK1 campaigns now run in one engine session. The test harness ends each level for real (thereisnotry: tally, Force screen, cutscene, next level). God mode on, audio muted.

Before Now
Continuous run from level 1 09FuelStation fails to load (largest block 2 MB) 01 → 21ajerec and the light ending; 01 → 21bjerec and the dark ending
Largest free PSRAM block at each level's load end 3.9 MB (light, from a fresh level 9); 1.0–2.0 MB (dark, from a fresh 15maw) 9.2 MB at every level from 02 on (8.1 MB at 09)
Lowest PSRAM free at a load end 1.3 MB (19A, dark) 9.6 MB (09FuelStation)
16bEscapeShip load end (dark) 1.44 MB free 11.3 MB free
Launcher PSRAM after quitting at the credits / during a load 9.1–11.7 MB 18.8 MB, as after boot

Cutscenes still play from resource/video\… (the cutscene path is unchanged). The patch was reviewed adversarially before testing (handle lifecycle, memory ownership, behaviour on other platforms), and no defects were found. The review flagged the existing short-read leak, which is fixed here too.

Not caused by this change: in a second JK session within one boot (any quit and relaunch), the first tally ignores Enter, though touch still works. The same happens without this change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

…o file

Before each cutscene, jkMain_cd_swap_reverify calls util_FileExists, which
goes through jkRes_FileOpen. Its directory walk tries episode\JK1 first:
stdGob_FileOpen's QOL path (stdEmbeddedRes_LoadOnlyInternal) found the loose
resource/video/X.smk and read the whole file into a malloc as a memory-mapped
GOB handle. The walk then reached "resource", where the low-level open also
succeeds, and replaced the slot's handle without closing the first one. So
the copy was never freed: 7.1 MB for 23A.SMK after level 8, 3.3 MB for 25A,
and so on for every cutscene small enough to fit (the 18-56 MB ones failed
the malloc). The memory was plain malloc, so it stayed through the engine's
shutdown too.

- jkRes_FileOpen: a later directory's loose file still replaces an earlier
  hit (cutscenes need the real "resource\video\..." path that
  jkRes_FileExists hands to smk_open_file), but the earlier handle is closed
  first, a FILE or a GOB handle.
- stdGob_FileOpen: the loaded copy is freed when no GOB handle is free.
- stdEmbeddedRes: a short read frees the copy and closes the file.
- ESP32: stdEmbeddedRes no longer reads loose files over 1 MB whole; jkRes
  opens them from resource/ as streams.

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