Skip to content

Fix window wrappers stealing GTK's toplevel reference - #6

Merged
makoni merged 1 commit into
mainfrom
fix/toplevel-window-ownership
Oct 13, 2026
Merged

makoni merged 1 commit into
mainfrom
fix/toplevel-window-ownership

Conversation

@makoni

@makoni makoni commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Window() and ApplicationWindow(application:) passed the transfer-ownership="none" pointer from adw_window_new / adw_application_window_new to init(raw:), which treats it as owned. The wrapper stole GTK's toplevel-list reference, so dropping it finalized a window GTK still listed, and the next toplevel walk (text direction or language switch, theme, inspector) hit freed memory: CRITICALs, then a SIGSEGV in gtk_widget_set_default_direction. Found through Swifty Notes, whose CI suite crashed in its language-switching tests after moving to 2.0.0.

Changes

  • Both initializers take their own reference before init(raw:). The generator emits the same for transfer-none toplevel constructors.
  • present() no longer calls retainUntilClose(), and retainUntilClose() is a no-op on windows. gtk_window_destroy doesn't emit "destroy" while another reference exists, so with a real reference that handler became a cycle and closed windows leaked (DemoLeakTests caught it).
  • Lifetime.md and the destroy() docs describe the new behaviour.

Tests

ToplevelWindowOwnershipTests (12): toplevel-list membership, finalization and zero CRITICALs during a toplevel walk, for Window and ApplicationWindow released before destroy, destroyed with a live wrapper, presented, and with an explicit retainUntilClose(). Full suite passes locally (1349 + 85), and Swifty Notes' CI set passes against this branch.

Not in this PR: retainUntilClose() on a Dialog has the same cycle and leaks after close(), tracked separately.

adw_window_new and adw_application_window_new are transfer none: the
one reference belongs to GTK's toplevel list. Window() and
ApplicationWindow(application:) handed it to init(raw:), which treats
the pointer as owned, so dropping the Swift wrapper finalized a window
GTK still listed. The next toplevel walk (a text-direction or language
switch, the theme, the inspector) then read freed memory: CRITICALs,
then a SIGSEGV in gtk_widget_set_default_direction. Both initializers
now take their own reference first; the generator emits the same for
any transfer-none toplevel constructor.

With a real reference, present()'s retainUntilClose() became a cycle:
gtk_window_destroy does not emit "destroy" while anyone still holds a
ref (it fires only at the last unref, checked in plain C), so the
handler capturing the wrapper was never released and closed windows
leaked (DemoLeakTests caught it). present() no longer retains, and
retainUntilClose() is a no-op on windows: GTK's toplevel reference
already keeps the wrapper alive through its toggle reference.

ToplevelWindowOwnershipTests covers Window and ApplicationWindow,
released before destroy, destroyed while the wrapper is alive,
presented, and with an explicit retainUntilClose(): toplevel-list
membership, finalization and zero CRITICALs during a toplevel walk.
@makoni
makoni merged commit c6b8a4c into main Oct 13, 2026
5 checks passed
@makoni
makoni deleted the fix/toplevel-window-ownership branch October 13, 2026 05:34
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