refactor: improve create pattern to avoid leaks

This commit is contained in:
CJ van den Berg 2025-07-19 00:03:30 +02:00
parent de68c1a5d4
commit efdad96054
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
46 changed files with 90 additions and 30 deletions

View file

@ -118,6 +118,7 @@ pub fn send(self: Self, from_: tp.pid_ref, m: tp.message) tp.result {
pub fn empty(allocator: Allocator) !Self { pub fn empty(allocator: Allocator) !Self {
const child: type = struct {}; const child: type = struct {};
const widget = try allocator.create(child); const widget = try allocator.create(child);
errdefer allocator.destroy(widget);
widget.* = .{}; widget.* = .{};
return .{ return .{
.ptr = widget, .ptr = widget,

View file

@ -144,6 +144,7 @@ const Process = struct {
return error.InvalidLspCommand; return error.InvalidLspCommand;
} }
const self = try allocator.create(Process); const self = try allocator.create(Process);
errdefer allocator.destroy(self);
var sp_tag_ = std.ArrayList(u8).init(allocator); var sp_tag_ = std.ArrayList(u8).init(allocator);
defer sp_tag_.deinit(); defer sp_tag_.deinit();
try sp_tag_.appendSlice(tag); try sp_tag_.appendSlice(tag);

View file

@ -159,6 +159,7 @@ pub const Leaf = struct {
if (piece.len == 0) if (piece.len == 0)
return if (!bol and !eol) &empty_leaf else if (bol and !eol) &empty_bol_leaf else if (!bol and eol) &empty_eol_leaf else &empty_line_leaf; return if (!bol and !eol) &empty_leaf else if (bol and !eol) &empty_bol_leaf else if (!bol and eol) &empty_eol_leaf else &empty_line_leaf;
const node = try allocator.create(Node); const node = try allocator.create(Node);
errdefer allocator.destroy(node);
node.* = .{ .leaf = .{ .buf = piece, .bol = bol, .eol = eol } }; node.* = .{ .leaf = .{ .buf = piece, .bol = bol, .eol = eol } };
return node; return node;
} }
@ -267,6 +268,7 @@ const Node = union(enum) {
fn new(allocator: Allocator, l: *const Node, r: *const Node) !*const Node { fn new(allocator: Allocator, l: *const Node, r: *const Node) !*const Node {
const node = try allocator.create(Node); const node = try allocator.create(Node);
errdefer allocator.destroy(node);
const l_weights_sum = l.weights_sum(); const l_weights_sum = l.weights_sum();
var weights_sum_ = Weights{}; var weights_sum_ = Weights{};
weights_sum_.add(l_weights_sum); weights_sum_.add(l_weights_sum);
@ -1065,6 +1067,7 @@ const Node = union(enum) {
pub fn create(allocator: Allocator) error{OutOfMemory}!*Self { pub fn create(allocator: Allocator) error{OutOfMemory}!*Self {
const self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
const arena_a = if (builtin.is_test) allocator else std.heap.page_allocator; const arena_a = if (builtin.is_test) allocator else std.heap.page_allocator;
self.* = .{ self.* = .{
.arena = std.heap.ArenaAllocator.init(arena_a), .arena = std.heap.ArenaAllocator.init(arena_a),

View file

@ -66,6 +66,7 @@ const Process = struct {
pub fn create() !tp.pid { pub fn create() !tp.pid {
const self = try allocator.create(Process); const self = try allocator.create(Process);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.receiver = Receiver.init(Process.receive, self), .receiver = Receiver.init(Process.receive, self),
}; };

View file

@ -65,7 +65,7 @@ const Handler = struct {
bindings: *const BindingSet, bindings: *const BindingSet,
fn create(mode_name: []const u8, allocator: std.mem.Allocator, opts: anytype) !Mode { fn create(mode_name: []const u8, allocator: std.mem.Allocator, opts: anytype) !Mode {
const self: *@This() = try allocator.create(@This()); const self = try allocator.create(@This());
errdefer allocator.destroy(self); errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,

View file

@ -263,6 +263,7 @@ const Process = struct {
fn create() SpawnError!tp.pid { fn create() SpawnError!tp.pid {
const allocator = std.heap.c_allocator; const allocator = std.heap.c_allocator;
const self = try allocator.create(Process); const self = try allocator.create(Process);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.parent = tp.self_pid().clone(), .parent = tp.self_pid().clone(),
@ -755,6 +756,7 @@ fn request_path_files_async(a_: std.mem.Allocator, parent_: tp.pid_ref, project_
fn spawn_link(allocator: std.mem.Allocator, parent: tp.pid_ref, project: *Project, max: usize, path: []const u8) (SpawnError || std.fs.Dir.OpenError)!void { fn spawn_link(allocator: std.mem.Allocator, parent: tp.pid_ref, project: *Project, max: usize, path: []const u8) (SpawnError || std.fs.Dir.OpenError)!void {
const self = try allocator.create(path_files); const self = try allocator.create(path_files);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.project_name = try allocator.dupe(u8, project.name), .project_name = try allocator.dupe(u8, project.name),

View file

@ -85,6 +85,7 @@ const Process = struct {
pub fn create(allocator: std.mem.Allocator, query: []const u8, tag: [:0]const u8, stdin_behavior: std.process.Child.StdIo) !tp.pid { pub fn create(allocator: std.mem.Allocator, query: []const u8, tag: [:0]const u8, stdin_behavior: std.process.Child.StdIo) !tp.pid {
const self = try allocator.create(Process); const self = try allocator.create(Process);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.query = try allocator.dupe(u8, query), .query = try allocator.dupe(u8, query),

View file

@ -45,6 +45,7 @@ const Process = struct {
pub fn create(allocator: std.mem.Allocator) Error!tp.pid { pub fn create(allocator: std.mem.Allocator) Error!tp.pid {
const self = try allocator.create(Process); const self = try allocator.create(Process);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.parent = tp.self_pid().clone(), .parent = tp.self_pid().clone(),

View file

@ -156,6 +156,7 @@ const Process = struct {
return error.InvalidShellArg0; return error.InvalidShellArg0;
const self = try allocator.create(Process); const self = try allocator.create(Process);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.argv = argv, .argv = argv,

View file

@ -62,6 +62,7 @@ pub const Error = CacheError || QueryParseError || QuerySerializeError;
pub fn create(allocator: std.mem.Allocator, opts: struct { lock: bool = false }) !*Self { pub fn create(allocator: std.mem.Allocator, opts: struct { lock: bool = false }) !*Self {
const self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.mutex = if (opts.lock) .{} else null, .mutex = if (opts.lock) .{} else null,

View file

@ -29,9 +29,13 @@ tree: ?*treez.Tree = null,
pub fn create(file_type: FileType, allocator: std.mem.Allocator, query_cache: *QueryCache) !*Self { pub fn create(file_type: FileType, allocator: std.mem.Allocator, query_cache: *QueryCache) !*Self {
const query = try query_cache.get(file_type, .highlights); const query = try query_cache.get(file_type, .highlights);
errdefer query_cache.release(query, .highlights);
const errors_query = try query_cache.get(file_type, .errors); const errors_query = try query_cache.get(file_type, .errors);
errdefer query_cache.release(errors_query, .highlights);
const injections = try query_cache.get(file_type, .injections); const injections = try query_cache.get(file_type, .injections);
errdefer if (injections) |injections_| query_cache.release(injections_, .injections);
const self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.lang = file_type.lang_fn() orelse std.debug.panic("tree-sitter parser function failed for language: {s}", .{file_type.name}), .lang = file_type.lang_fn() orelse std.debug.panic("tree-sitter parser function failed for language: {s}", .{file_type.name}),
@ -40,7 +44,6 @@ pub fn create(file_type: FileType, allocator: std.mem.Allocator, query_cache: *Q
.errors_query = errors_query, .errors_query = errors_query,
.injections = injections, .injections = injections,
}; };
errdefer self.destroy(query_cache);
try self.parser.setLanguage(self.lang); try self.parser.setLanguage(self.lang);
return self; return self;
} }
@ -58,6 +61,7 @@ pub fn static_create_guess_file_type_static(allocator: std.mem.Allocator, conten
pub fn destroy(self: *Self, query_cache: *QueryCache) void { pub fn destroy(self: *Self, query_cache: *QueryCache) void {
if (self.tree) |tree| tree.destroy(); if (self.tree) |tree| tree.destroy();
query_cache.release(self.query, .highlights); query_cache.release(self.query, .highlights);
query_cache.release(self.errors_query, .highlights);
if (self.injections) |injections| query_cache.release(injections, .injections); if (self.injections) |injections| query_cache.release(injections, .injections);
self.parser.destroy(); self.parser.destroy();
self.allocator.destroy(self); self.allocator.destroy(self);

View file

@ -285,6 +285,7 @@ pub const DeserializeError = error{
pub fn fromCbor(cb: []const u8, allocator: std.mem.Allocator) DeserializeError!struct { *TSQuery, *std.heap.ArenaAllocator } { pub fn fromCbor(cb: []const u8, allocator: std.mem.Allocator) DeserializeError!struct { *TSQuery, *std.heap.ArenaAllocator } {
var arena = try allocator.create(std.heap.ArenaAllocator); var arena = try allocator.create(std.heap.ArenaAllocator);
errdefer allocator.destroy(arena);
arena.* = std.heap.ArenaAllocator.init(allocator); arena.* = std.heap.ArenaAllocator.init(allocator);
errdefer arena.deinit(); errdefer arena.deinit();
const query = try arena.allocator().create(TSQuery); const query = try arena.allocator().create(TSQuery);

View file

@ -48,6 +48,7 @@ pub fn Options(context: type) type {
pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts: Options(ctx_type)) error{OutOfMemory}!*State(ctx_type) { pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts: Options(ctx_type)) error{OutOfMemory}!*State(ctx_type) {
const Self = State(ctx_type); const Self = State(ctx_type);
const self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
var n = try Plane.init(&opts.pos.opts(@typeName(Self)), parent); var n = try Plane.init(&opts.pos.opts(@typeName(Self)), parent);
errdefer n.deinit(); errdefer n.deinit();
self.* = .{ self.* = .{
@ -57,6 +58,7 @@ pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts:
.opts = opts, .opts = opts,
}; };
self.opts.label = try self.allocator.dupe(u8, opts.label); self.opts.label = try self.allocator.dupe(u8, opts.label);
errdefer allocator.free(self.opts.label);
try self.init(); try self.init();
return self; return self;
} }

View file

@ -58,9 +58,10 @@ pub fn Options(context: type) type {
pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts: Options(ctx_type)) !Widget { pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts: Options(ctx_type)) !Widget {
const Self = State(ctx_type); const Self = State(ctx_type);
const self = try allocator.create(Self);
var n = try Plane.init(&opts.pos.opts(@typeName(Self)), parent); var n = try Plane.init(&opts.pos.opts(@typeName(Self)), parent);
errdefer n.deinit(); errdefer n.deinit();
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.parent = parent, .parent = parent,
.plane = n, .plane = n,

View file

@ -56,6 +56,7 @@ pub fn Options(context: type) type {
pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts: Options(ctx_type)) !*State(ctx_type) { pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts: Options(ctx_type)) !*State(ctx_type) {
const self = try allocator.create(State(ctx_type)); const self = try allocator.create(State(ctx_type));
errdefer allocator.destroy(self);
const container = try WidgetList.createH(allocator, parent, @typeName(@This()), .dynamic); const container = try WidgetList.createH(allocator, parent, @typeName(@This()), .dynamic);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,

View file

@ -59,6 +59,7 @@ pub fn Options(context: type) type {
pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Widget, opts: Options(ctx_type)) !*State(ctx_type) { pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Widget, opts: Options(ctx_type)) !*State(ctx_type) {
const self = try allocator.create(State(ctx_type)); const self = try allocator.create(State(ctx_type));
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.plane = parent.plane.*, .plane = parent.plane.*,

View file

@ -224,6 +224,7 @@ pub fn hover(self: *Self) bool {
pub fn empty(allocator: Allocator, parent: Plane, layout_: Layout) !Self { pub fn empty(allocator: Allocator, parent: Plane, layout_: Layout) !Self {
const child: type = struct { plane: Plane, layout: Layout }; const child: type = struct { plane: Plane, layout: Layout };
const widget = try allocator.create(child); const widget = try allocator.create(child);
errdefer allocator.destroy(widget);
const n = try Plane.init(&(Box{}).opts("empty"), parent); const n = try Plane.init(&(Box{}).opts("empty"), parent);
widget.* = .{ .plane = n, .layout = layout_ }; widget.* = .{ .plane = n, .layout = layout_ };
return .{ return .{

View file

@ -32,21 +32,24 @@ after_render: *const fn (ctx: ?*anyopaque, theme: *const Widget.Theme) void = on
on_resize: *const fn (ctx: ?*anyopaque, self: *Self, pos_: Widget.Box) void = on_resize_default, on_resize: *const fn (ctx: ?*anyopaque, self: *Self, pos_: Widget.Box) void = on_resize_default,
pub fn createH(allocator: Allocator, parent: Plane, name: [:0]const u8, layout_: Layout) error{OutOfMemory}!*Self { pub fn createH(allocator: Allocator, parent: Plane, name: [:0]const u8, layout_: Layout) error{OutOfMemory}!*Self {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = try init(allocator, parent, name, .horizontal, layout_, Box{}); self.* = try init(allocator, parent, name, .horizontal, layout_, Box{});
self.plane.hide(); self.plane.hide();
return self; return self;
} }
pub fn createV(allocator: Allocator, parent: Plane, name: [:0]const u8, layout_: Layout) !*Self { pub fn createV(allocator: Allocator, parent: Plane, name: [:0]const u8, layout_: Layout) !*Self {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = try init(allocator, parent, name, .vertical, layout_, Box{}); self.* = try init(allocator, parent, name, .vertical, layout_, Box{});
self.plane.hide(); self.plane.hide();
return self; return self;
} }
pub fn createBox(allocator: Allocator, parent: Plane, name: [:0]const u8, dir: Direction, layout_: Layout, box: Box) !*Self { pub fn createBox(allocator: Allocator, parent: Plane, name: [:0]const u8, dir: Direction, layout_: Layout, box: Box) !*Self {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = try init(allocator, parent, name, dir, layout_, box); self.* = try init(allocator, parent, name, dir, layout_, box);
self.plane.hide(); self.plane.hide();
return self; return self;

View file

@ -5941,7 +5941,8 @@ pub const EditorWidget = struct {
fn create(allocator: Allocator, parent: Plane, buffer_manager: *Buffer.Manager) !Widget { fn create(allocator: Allocator, parent: Plane, buffer_manager: *Buffer.Manager) !Widget {
const container = try WidgetList.createH(allocator, parent, "editor.container", .dynamic); const container = try WidgetList.createH(allocator, parent, "editor.container", .dynamic);
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
try self.init(allocator, container.plane, buffer_manager); try self.init(allocator, container.plane, buffer_manager);
try self.commands.init(&self.editor); try self.commands.init(&self.editor);
const editorWidget = Widget.to(self); const editorWidget = Widget.to(self);

View file

@ -43,7 +43,8 @@ const Kind = enum { insert, modified, delete };
const Symbol = struct { kind: Kind, line: usize }; const Symbol = struct { kind: Kind, line: usize };
pub fn create(allocator: Allocator, parent: Widget, event_source: Widget, editor: *ed.Editor) !Widget { pub fn create(allocator: Allocator, parent: Widget, event_source: Widget, editor: *ed.Editor) !Widget {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent.plane.*), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent.plane.*),

View file

@ -47,7 +47,8 @@ const Entry = struct {
}; };
pub fn create(allocator: Allocator, parent: Plane) !Widget { pub fn create(allocator: Allocator, parent: Plane) !Widget {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.plane = try Plane.init(&(Widget.Box{}).opts(name), parent), .plane = try Plane.init(&(Widget.Box{}).opts(name), parent),

View file

@ -81,7 +81,8 @@ const Self = @This();
pub fn create(allocator: std.mem.Allocator, parent: Widget) !Widget { pub fn create(allocator: std.mem.Allocator, parent: Widget) !Widget {
const logger = log.logger("home"); const logger = log.logger("home");
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
var n = try Plane.init(&(Widget.Box{}).opts("editor"), parent.plane.*); var n = try Plane.init(&(Widget.Box{}).opts("editor"), parent.plane.*);
errdefer n.deinit(); errdefer n.deinit();
@ -303,6 +304,8 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
self.position_menu(self.v_center(5, self.menu_len, 5), self.center(x, self.menu_w)); self.position_menu(self.v_center(5, self.menu_len, 5), self.center(x, self.menu_w));
} }
if (self.plane.dim_y() < 3 or self.plane.dim_x() < root.version.len + 4) return false;
self.plane.cursor_move_yx( self.plane.cursor_move_yx(
@intCast(self.plane.dim_y() - 2), @intCast(self.plane.dim_y() - 2),
@intCast(@max(self.plane.dim_x(), root.version.len + 3) - root.version.len - 3), @intCast(@max(self.plane.dim_x(), root.version.len + 3) - root.version.len - 3),
@ -311,6 +314,7 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
_ = self.plane.print("{s}", .{root.version}) catch return false; _ = self.plane.print("{s}", .{root.version}) catch return false;
if (builtin.mode == .Debug) { if (builtin.mode == .Debug) {
const debug_warning_text = "debug build"; const debug_warning_text = "debug build";
if (self.plane.dim_y() < 4 or self.plane.dim_x() < debug_warning_text.len + 4) return false;
self.plane.cursor_move_yx( self.plane.cursor_move_yx(
@intCast(self.plane.dim_y() - 3), @intCast(self.plane.dim_y() - 3),
@intCast(@max(self.plane.dim_x(), debug_warning_text.len + 3) - debug_warning_text.len - 3), @intCast(@max(self.plane.dim_x(), debug_warning_text.len + 3) - debug_warning_text.len - 3),

View file

@ -14,7 +14,8 @@ view_rows: usize = 0,
lines: std.ArrayList([]const u8), lines: std.ArrayList([]const u8),
pub fn create(allocator: Allocator, parent: Plane) !Widget { pub fn create(allocator: Allocator, parent: Plane) !Widget {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.plane = try Plane.init(&(Widget.Box{}).opts(name), parent), .plane = try Plane.init(&(Widget.Box{}).opts(name), parent),

View file

@ -30,9 +30,10 @@ const Entry = struct {
const Buffer = ArrayList(Entry); const Buffer = ArrayList(Entry);
pub fn create(allocator: Allocator, parent: Plane) !Widget { pub fn create(allocator: Allocator, parent: Plane) !Widget {
const self: *Self = try allocator.create(Self);
var n = try Plane.init(&(Widget.Box{}).opts_vscroll(@typeName(Self)), parent); var n = try Plane.init(&(Widget.Box{}).opts_vscroll(@typeName(Self)), parent);
errdefer n.deinit(); errdefer n.deinit();
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.parent = parent, .parent = parent,

View file

@ -26,7 +26,8 @@ const Self = @This();
pub fn create(allocator: Allocator, parent: Plane) !Widget { pub fn create(allocator: Allocator, parent: Plane) !Widget {
const editor = tui.get_active_editor() orelse return error.NotFound; const editor = tui.get_active_editor() orelse return error.NotFound;
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts_vscroll(name), parent), .plane = try Plane.init(&(Widget.Box{}).opts_vscroll(name), parent),
.editor = editor, .editor = editor,

View file

@ -38,7 +38,8 @@ const Level = enum {
}; };
pub fn create(allocator: Allocator, parent: Plane) !Widget { pub fn create(allocator: Allocator, parent: Plane) !Widget {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ .plane = try Plane.init(&(Widget.Box{}).opts(name), parent) }; self.* = .{ .plane = try Plane.init(&(Widget.Box{}).opts(name), parent) };
return Widget.to(self); return Widget.to(self);
} }

View file

@ -66,6 +66,7 @@ pub const CreateError = error{ OutOfMemory, ThespianSpawnFailed };
pub fn create(allocator: std.mem.Allocator) CreateError!Widget { pub fn create(allocator: std.mem.Allocator) CreateError!Widget {
const self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.plane = tui.plane(), .plane = tui.plane(),

View file

@ -21,7 +21,8 @@ pub fn Create(options: type) type {
const Self = @This(); const Self = @This();
pub fn create(allocator: std.mem.Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { pub fn create(allocator: std.mem.Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.input = std.ArrayList(u8).init(allocator), .input = std.ArrayList(u8).init(allocator),

View file

@ -35,7 +35,8 @@ pub fn Create(options: type) type {
}; };
pub fn create(allocator: std.mem.Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { pub fn create(allocator: std.mem.Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.file_path = std.ArrayList(u8).init(allocator), .file_path = std.ArrayList(u8).init(allocator),

View file

@ -28,7 +28,8 @@ commands: Commands = undefined,
pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
const editor = tui.get_active_editor() orelse return error.NotFound; const editor = tui.get_active_editor() orelse return error.NotFound;
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.input_ = ArrayList(u8).init(allocator), .input_ = ArrayList(u8).init(allocator),

View file

@ -25,7 +25,8 @@ last_input: []u8 = "",
commands: Commands = undefined, commands: Commands = undefined,
pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ .allocator = allocator }; self.* = .{ .allocator = allocator };
try self.commands.init(self); try self.commands.init(self);
if (tui.get_active_selection(self.allocator)) |text| { if (tui.get_active_selection(self.allocator)) |text| {

View file

@ -24,8 +24,9 @@ start: usize,
commands: Commands = undefined, commands: Commands = undefined,
pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
const self: *Self = try allocator.create(Self);
const editor = tui.get_active_editor() orelse return error.NotFound; const editor = tui.get_active_editor() orelse return error.NotFound;
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.start = editor.get_primary().cursor.row + 1, .start = editor.get_primary().cursor.row + 1,

View file

@ -38,7 +38,8 @@ pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tu
const direction: Direction = if (std.mem.indexOf(u8, operation_command, "_left")) |_| .left else .right; const direction: Direction = if (std.mem.indexOf(u8, operation_command, "_left")) |_| .left else .right;
const operation: Operation = if (tui.get_active_editor()) |editor| if (editor.get_primary().selection) |_| .select else .move else .move; const operation: Operation = if (tui.get_active_editor()) |editor| if (editor.get_primary().selection) |_| .select else .move else .move;
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.direction = direction, .direction = direction,

View file

@ -38,7 +38,8 @@ buffer_manager: ?*BufferManager,
pub fn create(allocator: std.mem.Allocator) !tui.Mode { pub fn create(allocator: std.mem.Allocator) !tui.Mode {
const mv = tui.mainview() orelse return error.NotFound; const mv = tui.mainview() orelse return error.NotFound;
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.modal = try ModalBackground.create(*Self, allocator, tui.mainview_widget(), .{ .ctx = self }), .modal = try ModalBackground.create(*Self, allocator, tui.mainview_widget(), .{ .ctx = self }),

View file

@ -47,7 +47,8 @@ pub fn Create(options: type) type {
pub fn create(allocator: std.mem.Allocator) !tui.Mode { pub fn create(allocator: std.mem.Allocator) !tui.Mode {
const mv = tui.mainview() orelse return error.NotFound; const mv = tui.mainview() orelse return error.NotFound;
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.modal = try ModalBackground.create(*Self, allocator, tui.mainview_widget(), .{ .modal = try ModalBackground.create(*Self, allocator, tui.mainview_widget(), .{

View file

@ -27,7 +27,8 @@ style_factory: ?*const fn (self: *Self, theme: *const Widget.Theme) Widget.Theme
const Self = @This(); const Self = @This();
pub fn create(allocator: Allocator, parent: Plane, event_source: ?Widget, event_sink: EventHandler) !Widget { pub fn create(allocator: Allocator, parent: Plane, event_source: ?Widget, event_sink: EventHandler) !Widget {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
.event_sink = event_sink, .event_sink = event_sink,

View file

@ -20,7 +20,8 @@ pub fn Create(comptime layout_: Widget.Layout) @import("widget.zig").CreateFunct
break :blk Widget.Layout{ .static = size }; break :blk Widget.Layout{ .static = size };
} else break :blk layout_; } else break :blk layout_;
} else layout_; } else layout_;
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
.layout_ = layout__, .layout_ = layout__,

View file

@ -30,7 +30,8 @@ pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?Event
return error.WidgetInitFailed; return error.WidgetInitFailed;
}; };
defer env.deinit(); defer env.deinit();
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),

View file

@ -12,7 +12,8 @@ plane: Plane,
const Self = @This(); const Self = @This();
pub fn create(allocator: std.mem.Allocator, parent: Plane, _: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget { pub fn create(allocator: std.mem.Allocator, parent: Plane, _: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),

View file

@ -31,7 +31,8 @@ pub const width = idle_msg.len + 20;
pub fn create(allocator: Allocator, parent: Plane, _: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget { pub fn create(allocator: Allocator, parent: Plane, _: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget {
const frame_rate = tp.env.get().num("frame-rate"); const frame_rate = tp.env.get().num("frame-rate");
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
.wipe_after_frames = @divTrunc(frame_rate, 2), .wipe_after_frames = @divTrunc(frame_rate, 2),

View file

@ -27,7 +27,8 @@ const Level = enum {
}; };
pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget { pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
.msg = std.ArrayList(u8).init(allocator), .msg = std.ArrayList(u8).init(allocator),

View file

@ -20,7 +20,8 @@ const Self = @This();
pub const width = 8; pub const width = 8;
pub fn create(allocator: Allocator, parent: Plane, _: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget { pub fn create(allocator: Allocator, parent: Plane, _: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
}; };

View file

@ -19,7 +19,8 @@ on_event: ?EventHandler,
const Self = @This(); const Self = @This();
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget { pub fn create(allocator: Allocator, parent: Plane, event_handler: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget {
const self: *Self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
.on_event = event_handler, .on_event = event_handler,

View file

@ -54,6 +54,7 @@ pub const Style = @"style.config";
pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget { pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget {
const self = try allocator.create(TabBar); const self = try allocator.create(TabBar);
errdefer allocator.destroy(self);
self.* = try TabBar.init(allocator, parent, event_handler); self.* = try TabBar.init(allocator, parent, event_handler);
return Widget.to(self); return Widget.to(self);
} }
@ -447,6 +448,7 @@ const spacer = struct {
event_handler: ?EventHandler, event_handler: ?EventHandler,
) @import("widget.zig").CreateError!Widget { ) @import("widget.zig").CreateError!Widget {
const self: *Self = try allocator.create(Self); const self: *Self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
.layout_ = .{ .static = self.plane.egc_chunk_width(content, 0, 1) }, .layout_ = .{ .static = self.plane.egc_chunk_width(content, 0, 1) },

View file

@ -120,6 +120,9 @@ fn init(allocator: Allocator) InitError!*Self {
const frame_clock = try tp.metronome.init(frame_time); const frame_clock = try tp.metronome.init(frame_time);
var self = try allocator.create(Self); var self = try allocator.create(Self);
// don't destroy
// if tui fails it is catastrophic anyway and we don't want to cause nock-on errors
// errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.config_ = conf, .config_ = conf,

View file

@ -21,6 +21,7 @@ pub fn start(a_: std.mem.Allocator, root_path_: []const u8) (SpawnError || std.f
fn spawn_link(allocator: std.mem.Allocator, root_path: []const u8) (SpawnError || std.fs.Dir.OpenError)!tp.pid { fn spawn_link(allocator: std.mem.Allocator, root_path: []const u8) (SpawnError || std.fs.Dir.OpenError)!tp.pid {
const self = try allocator.create(tree_walker); const self = try allocator.create(tree_walker);
errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.root_path = try allocator.dupe(u8, root_path), .root_path = try allocator.dupe(u8, root_path),