Skip to content

gtk3: Release the GtkFileChooserNative reference on drop - #366

Open
Akmot9 wants to merge 1 commit into
PolyMeilex:masterfrom
Akmot9:gtk3-unref-file-chooser-native
Open

Akmot9 wants to merge 1 commit into
PolyMeilex:masterfrom
Akmot9:gtk3-unref-file-chooser-native

Conversation

@Akmot9

@Akmot9 Akmot9 commented Nov 20, 2026

Copy link
Copy Markdown

Fixes #365.

gtk_file_chooser_native_new returns a reference owned by the caller, but GtkFileDialog::drop only called gtk_native_dialog_destroy, which does not release it (see the note in gtknativedialog.c). In GTK 3, GtkFileChooserNative destroys its fallback GtkFileChooserDialog only in finalize. So every GTK3 file dialog (sync and async, all pickers and save_file) left a hidden GtkFileChooserDialog alive. This adds the missing g_object_unref after gtk_native_dialog_destroy.

The async path drops the dialog from inside its own response handler. GLib holds a reference on the instance for the duration of the emission, so finalization happens after the handler returns.

Testing

I ran the program from #365 (30 dialogs, each cancelled from a GLib timeout) plus an AsyncFileDialog variant run with pollster::block_on, on Xvfb with GTK 3.24.33 (Ubuntu 22.04):

master (2d35d7d), sync
after   1 dialogs:   1 hidden GtkFileChooserDialog alive, RSS 95764 KiB
after  10 dialogs:  10 hidden GtkFileChooserDialog alive, RSS 106132 KiB
after  20 dialogs:  20 hidden GtkFileChooserDialog alive, RSS 117652 KiB
after  30 dialogs:  30 hidden GtkFileChooserDialog alive, RSS 128660 KiB

master (2d35d7d), async
after   1 dialogs:   1 hidden GtkFileChooserDialog alive, RSS 95788 KiB
after  10 dialogs:  10 hidden GtkFileChooserDialog alive, RSS 106028 KiB
after  20 dialogs:  20 hidden GtkFileChooserDialog alive, RSS 118060 KiB
after  30 dialogs:  30 hidden GtkFileChooserDialog alive, RSS 128940 KiB

this branch, sync
after   1 dialogs:   0 hidden GtkFileChooserDialog alive, RSS 96668 KiB
after  10 dialogs:   0 hidden GtkFileChooserDialog alive, RSS 96796 KiB
after  20 dialogs:   0 hidden GtkFileChooserDialog alive, RSS 96796 KiB
after  30 dialogs:   0 hidden GtkFileChooserDialog alive, RSS 96796 KiB

this branch, async
after   1 dialogs:   0 hidden GtkFileChooserDialog alive, RSS 96284 KiB
after  10 dialogs:   0 hidden GtkFileChooserDialog alive, RSS 96540 KiB
after  20 dialogs:   0 hidden GtkFileChooserDialog alive, RSS 96540 KiB
after  30 dialogs:   0 hidden GtkFileChooserDialog alive, RSS 96540 KiB

There were no GTK criticals or warnings in either run. cargo fmt --check, cargo build and cargo test with --no-default-features --features gtk3 pass, and cargo clippy reports the same warnings as on master.

🤖 Generated with Claude Code

`gtk_file_chooser_native_new` returns a reference owned by the caller,
but `GtkFileDialog::drop` only called `gtk_native_dialog_destroy`, which
does not release it. In GTK 3 the native dialog destroys its fallback
`GtkFileChooserDialog` only when finalized, so every file dialog left a
hidden `GtkFileChooserDialog` alive.

Fixes PolyMeilex#365

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PolyMeilex

Copy link
Copy Markdown
Owner

Could you run https://github.com/PolyMeilex/rfd/tree/master/examples%2Fgtk-threading-manual-tests
as well, just to be sure we don't regress.

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.

gtk3: GtkFileChooserNative is never unreffed, so every file dialog leaks a hidden GtkFileChooserDialog

2 participants