Repository navigation
ESP32: checking that a cutscene exists no longer leaks the whole video file - #3
Merged
Merged
Conversation
…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
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.
Checking that a cutscene exists leaked the whole video file
Before each cutscene,
jkMain_cd_swap_reverifycallsutil_FileExists("video\X.smk"), which goes throughjkRes_FileOpen. That function walks the GOB directories in order:episode\JK1the low-level open fails, so it callsstdGob_FileOpen(JK1.GOB, …).QOL_IMPROVEMENTS, that function first callsstdEmbeddedRes_LoadOnlyInternal. It found the looseresource/video/X.smkand read the whole file into amallocas a memory-mapped GOB handle.resourcethe 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_FileExistshandssmk_open_filethe slot's path, and only the low-level hit has the realresource\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.stdEmbeddedResno longer reads loose files over 1 MB whole.jkResopens them fromresource/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.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