Prevent lua from Garbage Collecting possibly-stale Subframes#6866
Merged
BMagnu merged 1 commit intoJul 25, 2025
Conversation
wookieejedi
approved these changes
Jul 25, 2025
wookieejedi
left a comment
Member
There was a problem hiding this comment.
Thanks for all your work on tracking down and making this fix!
Kestrellius
pushed a commit
to Kestrellius/fs2open.github.com
that referenced
this pull request
Jul 26, 2025
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.
The bug that was observed before this fix abbreviates to roughly the following sequence of events.
A lua variable is held, containing an animated texture.
A
gr.drawImagecall is used to draw a specific frame of that animation by indexing into the above variable.This leaves the temporary returned from indexing with a newer last-used timestamp than the actual animation-holding variable.
It is now possible that the animation-holding variable is unloaded or garbage collected. This frees the according slots in bmpman, allowing them to be filled with other data.
At the same time, the temporary is not yet garbage collected. Once that happens (e.g. in the forced garbage collection pass during mission load) the subframe temporary will try to deallocate itself in bmpman. But now, that slot is already used for a different, unrelated texture (which, in the mission load case, has a good chance of being the mission loading bar animation), which'll be deleted incorrectly, causing problems down the road where other parts of the code now hold a bmpman handle that's no longer valid.
The fix to this is that lua textures which are not first-class citizens in bmpman, but are rather just subtextures of a parent texture should not try to alloc / dealloc themselves, but their parent container texture. Not only does this fix incorrect deallocations, but it also prevents a texture that's held in lua only to be deallocated, even if we still have references to some frames of this texture.