Repository navigation
Fix window wrappers stealing GTK's toplevel reference - #6
Merged
Merged
Conversation
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.
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.
Window()andApplicationWindow(application:)passed thetransfer-ownership="none"pointer fromadw_window_new/adw_application_window_newtoinit(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 ingtk_widget_set_default_direction. Found through Swifty Notes, whose CI suite crashed in its language-switching tests after moving to 2.0.0.Changes
init(raw:). The generator emits the same for transfer-none toplevel constructors.present()no longer callsretainUntilClose(), andretainUntilClose()is a no-op on windows.gtk_window_destroydoesn't emit "destroy" while another reference exists, so with a real reference that handler became a cycle and closed windows leaked (DemoLeakTestscaught it).Lifetime.mdand thedestroy()docs describe the new behaviour.Tests
ToplevelWindowOwnershipTests(12): toplevel-list membership, finalization and zero CRITICALs during a toplevel walk, forWindowandApplicationWindowreleased before destroy, destroyed with a live wrapper, presented, and with an explicitretainUntilClose(). Full suite passes locally (1349 + 85), and Swifty Notes' CI set passes against this branch.Not in this PR:
retainUntilClose()on aDialoghas the same cycle and leaks afterclose(), tracked separately.