apprt/gtk-ng: fix config textbuffer memory leak

This commit is contained in:
Mitchell Hashimoto
2025-07-17 13:17:51 -07:00
parent 8556877883
commit 9b99e41cb2
5 changed files with 47 additions and 6 deletions

View File

@ -6,10 +6,7 @@ pub const Surface = @import("gtk-ng/Surface.zig");
pub const resourcesDir = internal_os.resourcesDir;
// The exported API, custom for the apprt.
pub const Application = @import("gtk-ng/class/application.zig").Application;
pub const Window = @import("gtk-ng/class/window.zig").Window;
pub const Config = @import("gtk-ng/class/config.zig").Config;
pub const class = @import("gtk-ng/class.zig");
pub const WeakRef = @import("gtk-ng/weak_ref.zig").WeakRef;
test {

View File

@ -0,0 +1,26 @@
//! This files contains all the GObject classes for the GTK apprt
//! along with helpers to work with them.
const glib = @import("glib");
const gobject = @import("gobject");
pub const Application = @import("class/application.zig").Application;
pub const Window = @import("class/window.zig").Window;
pub const Config = @import("class/config.zig").Config;
/// Unrefs the given GObject on the next event loop tick.
///
/// This works around an issue with zig-object where dynamically
/// generated gobjects in property getters can't unref themselves
/// normally: https://github.com/ianprime0509/zig-gobject/issues/108
pub fn unrefLater(obj: anytype) void {
_ = glib.idleAdd((struct {
fn callback(data_: ?*anyopaque) callconv(.c) c_int {
const remove = @intFromBool(glib.SOURCE_REMOVE);
const data = data_ orelse return remove;
const object: *gobject.Object = @ptrCast(@alignCast(data));
object.unref();
return remove;
}
}).callback, obj.as(gobject.Object));
}

View File

@ -585,8 +585,8 @@ pub const Application = extern struct {
self.as(Parent),
);
const win = Window.new(self);
gtk.Window.present(win.as(gtk.Window));
// const win = Window.new(self);
// gtk.Window.present(win.as(gtk.Window));
}
fn dispose(self: *Self) callconv(.C) void {

View File

@ -1,12 +1,15 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const adw = @import("adw");
const glib = @import("glib");
const gobject = @import("gobject");
const gtk = @import("gtk");
const configpkg = @import("../../../config.zig");
const CoreConfig = configpkg.Config;
const unrefLater = @import("../class.zig").unrefLater;
const log = std.log.scoped(.gtk_ghostty_config);
/// Wraps a `Ghostty.Config` object in a GObject so it can be reference
@ -125,6 +128,7 @@ pub const Config = extern struct {
text_buf.insertAtCursor("\n", 1);
}
unrefLater(text_buf); // See unrefLater docs for why this is needed
return text_buf;
}

View File

@ -140,6 +140,20 @@
...
}
{
pango font map to cairgo
Memcheck:Leak
match-leak-kinds: possible
fun:malloc
...
fun:FcConfigValues
fun:FcConfigSubstituteWithPat
fun:FcConfigSubstitute
fun:pango_cairo_fc_font_map_fontset_key_substitute
fun:pango_fc_font_map_load_fontset
...
}
{
Adwaita Stylesheet Load
Memcheck:Leak