Skip to content

video: unify screen-surface teardown into one helper - #3

Open
ForkedInTime wants to merge 1 commit into
alphafrom
harden/screen-surface-teardown
Open

video: unify screen-surface teardown into one helper#3
ForkedInTime wants to merge 1 commit into
alphafrom
harden/screen-surface-teardown

Conversation

@ForkedInTime

Copy link
Copy Markdown
Owner

What

Extract the duplicated screen-surface / GL-backend teardown in bflib_video.c into a single lb_release_screen_surfaces() helper, and route both LbScreenSetup and LbScreenReset through it.

Why

The two functions each open-coded the same teardown, and they had drifted:

  • LbScreenReset cleared both lbDrawSurface and lbScreenSurface.
  • LbScreenSetup cleared only lbDrawSurface.

In GL-present mode those two globals alias the same owned surface, so after LbScreenSetup's teardown lbScreenSurface was left dangling at a just-freed surface — and stayed dangling on any early-return failure path (SDL_SetWindowDisplayMode/SDL_SetWindowFullscreen/window-create failure) before the new surface is assigned.

This is latent hardening: it was not the cause of the recent level-transition crash (that was a config/packaging issue, fixed separately), and repeated in-game resolution switches (Alt+R, the same teardown path) don't trip it in practice. But the asymmetry is a real dangling-pointer hazard and an easy trap for the next change.

Changes

  • New static void lb_release_screen_surfaces(void) — frees the draw/second surface (and shuts down the GL backend if active), then clears lbHasSecondSurface, lbDrawSurface, lbScreenSurface, and lbScreenInitialised. Documents why both surface pointers must be cleared (GL alias) and why the CPU window-surface must not be freed.
  • LbScreenSetup and LbScreenReset now call the helper instead of duplicating it — they can't diverge again.
  • Removed the dead prevScreenSurf capture + empty if (prevScreenSurf != NULL) {} block in LbScreenSetup.
  • Restructured the GL/second-surface free with a freed_draw_surface flag to drop the #ifndef _WIN32-straddling else (clears a clang-tidy misleading-indentation warning).

Verification

Behavior-preserving on the happy path; the only functional change is nulling lbScreenSurface in the setup teardown (strictly safer — it's reassigned before use, or left NULL instead of dangling on failure). Engine builds clean (make -f linux.mk, no new warnings on bflib_video.c).

🤖 Generated with Claude Code

LbScreenSetup and LbScreenReset each open-coded the same surface/GL
teardown, and they had drifted: LbScreenReset cleared both lbDrawSurface
and lbScreenSurface, but LbScreenSetup cleared only lbDrawSurface. In
GL-present mode those two globals alias the same owned surface, so after
LbScreenSetup's teardown lbScreenSurface was left dangling at a freed
surface (and stayed dangling on any early-return failure path before the
new surface is created).

Extract the shared logic into lb_release_screen_surfaces() and route both
callers through it, so they can't diverge again. This nulls lbScreenSurface
in the setup path too. Also drop the dead 'prevScreenSurf' capture + empty
if-block in LbScreenSetup, and restructure the GL/second-surface free to
avoid the #ifndef-straddling else (clang-tidy misleading-indentation).

Behavior-preserving hardening; no functional change on the happy path.
Builds clean.
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