pub fn fromFile(io: std.Io, allocator: std.mem.Allocator, file_path: []const u8) !gcat.Arena(Config) {
const arena = try allocator.create(std.heap.ArenaAllocator);
errdefer allocator.destroy(arena);
arena.* = .init(allocator);
errdefer arena.deinit();
const config_bytes = try std.Io.Dir.cwd().readFileAllocOptions(
io,
file_path,
arena.allocator(),
.unlimited,
.@"1",
0,
);
var diag: std.zon.parse.Diagnostics = .{};
defer diag.deinit(allocator);
const config = std.zon.parse.fromSliceAlloc(Config, arena.allocator(), config_bytes, &diag, .{}) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
error.ParseZon => |e| {
gcat.logger.err("failed to parse config file(zon): {f}", .{diag});
return e;
},
};
try config.validate();
return gcat.Arena(Config){ .arena = arena, .value = config };
}
Hint
thread 120945 panic: Invalid free
And that’s why we use DebugAllocator folks!!
And as always, if you know a way to help prevent this type of bug, let me know!
the zon api allows custom parse functions for types, and some types need to store the allocator, heap allocating the arena is required to make that safe.
– vulpesxAh yes of course, good point
– pasta