Skip to content

Prevent lua from Garbage Collecting possibly-stale Subframes#6866

Merged
BMagnu merged 1 commit into
scp-fs2open:masterfrom
BMagnu:lua_subframe_garbage_collection
Jul 25, 2025
Merged

Prevent lua from Garbage Collecting possibly-stale Subframes#6866
BMagnu merged 1 commit into
scp-fs2open:masterfrom
BMagnu:lua_subframe_garbage_collection

Conversation

@BMagnu

@BMagnu BMagnu commented Jul 25, 2025

Copy link
Copy Markdown
Member

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.drawImage call 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.

@BMagnu BMagnu added this to the Release 25.0 milestone Jul 25, 2025
@BMagnu BMagnu added bug An issue from unintended consequences graphics A feature or issue related to graphics (2d and 3d) scripting A feature or issue related to LUA scripting labels Jul 25, 2025

@wookieejedi wookieejedi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all your work on tracking down and making this fix!

@BMagnu BMagnu merged commit 72ed863 into scp-fs2open:master Jul 25, 2025
20 checks passed
Kestrellius pushed a commit to Kestrellius/fs2open.github.com that referenced this pull request Jul 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue from unintended consequences graphics A feature or issue related to graphics (2d and 3d) scripting A feature or issue related to LUA scripting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants