gtk-ng: fix memory leaks in command palette

pull/8182/head
Jeffrey C. Ollie 2025-08-08 21:07:54 -05:00 committed by Mitchell Hashimoto
parent 3221421a74
commit 5c088d10a4
2 changed files with 20 additions and 5 deletions

View File

@ -174,14 +174,18 @@ pub const CommandPalette = extern struct {
} }
} }
fn dialogClosed(_: adw.Dialog, self: *CommandPalette) callconv(.c) void { fn close(self: *CommandPalette) void {
const priv = self.private();
_ = priv.dialog.close();
}
fn dialogClosed(_: *adw.Dialog, self: *CommandPalette) callconv(.c) void {
self.unref(); self.unref();
} }
fn searchStopped(_: *gtk.SearchEntry, self: *CommandPalette) callconv(.c) void { fn searchStopped(_: *gtk.SearchEntry, self: *CommandPalette) callconv(.c) void {
// ESC was pressed - close the palette // ESC was pressed - close the palette
const priv = self.private(); self.close();
_ = priv.dialog.close();
} }
fn searchActivated(_: *gtk.SearchEntry, self: *CommandPalette) callconv(.c) void { fn searchActivated(_: *gtk.SearchEntry, self: *CommandPalette) callconv(.c) void {
@ -203,7 +207,7 @@ pub const CommandPalette = extern struct {
// If the dialog has been shown, close it. // If the dialog has been shown, close it.
if (priv.dialog.as(gtk.Widget).getRealized() != 0) { if (priv.dialog.as(gtk.Widget).getRealized() != 0) {
_ = priv.dialog.close(); self.close();
return; return;
} }
@ -227,7 +231,7 @@ pub const CommandPalette = extern struct {
// another dialog (such as the change title dialog). If that occurs then // another dialog (such as the change title dialog). If that occurs then
// the command palette dialog won't be counted as having closed properly // the command palette dialog won't be counted as having closed properly
// and cannot receive focus when reopened. // and cannot receive focus when reopened.
_ = priv.dialog.close(); self.close();
const cmd = gobject.ext.cast(Command, object_ orelse return) orelse return; const cmd = gobject.ext.cast(Command, object_ orelse return) orelse return;
const action = cmd.getAction() orelse return; const action = cmd.getAction() orelse return;

View File

@ -1064,10 +1064,18 @@ pub const Window = extern struct {
fn dispose(self: *Self) callconv(.c) void { fn dispose(self: *Self) callconv(.c) void {
const priv = self.private(); const priv = self.private();
command_palette: {
// TODO: this can be simplified once WeakRef.get() can return a null.
const command_palette = gobject.ext.cast(CommandPalette, priv.command_palette.get()) orelse break :command_palette;
command_palette.unref();
}
if (priv.config) |v| { if (priv.config) |v| {
v.unref(); v.unref();
priv.config = null; priv.config = null;
} }
priv.tab_bindings.setSource(null); priv.tab_bindings.setSource(null);
gtk.Widget.disposeTemplate( gtk.Widget.disposeTemplate(
@ -1708,9 +1716,12 @@ pub const Window = extern struct {
/// TODO: accept the surface that toggled the command palette as a parameter /// TODO: accept the surface that toggled the command palette as a parameter
fn toggleCommandPalette(self: *Window) void { fn toggleCommandPalette(self: *Window) void {
const priv = self.private(); const priv = self.private();
// Get a reference to a command palette. First check the weak reference // Get a reference to a command palette. First check the weak reference
// that we save to see if we already have one stored. If we don't then // that we save to see if we already have one stored. If we don't then
// create a new one. // create a new one.
//
// TODO: once WeakRef.get() can return a null this will need to be fixed up.
const command_palette = gobject.ext.cast(CommandPalette, priv.command_palette.get()) orelse command_palette: { const command_palette = gobject.ext.cast(CommandPalette, priv.command_palette.get()) orelse command_palette: {
// Create a fresh command palette. // Create a fresh command palette.
const command_palette = CommandPalette.new(); const command_palette = CommandPalette.new();