diff --git a/examples/simple.zig b/examples/simple.zig index bf85656..24eee50 100644 --- a/examples/simple.zig +++ b/examples/simple.zig @@ -44,7 +44,8 @@ fn sub3(r: *cli.AppRunner) !cli.Command { } fn parseArgs() cli.AppRunner.Error!cli.ExecFn { - var r = try cli.AppRunner.init(std.heap.page_allocator); + // This allocator will be used to allocate config.ip and config.arg2. + var r = try cli.AppRunner.init(allocator); const sub2 = cli.Command{ .name = "sub2", @@ -115,7 +116,25 @@ fn parseArgs() cli.AppRunner.Error!cli.ExecFn { pub fn main() anyerror!void { const action = try parseArgs(); - return action(); + const r = action(); + freeConfig(); + return r; +} + +// Usually, you just use an arena allocator to free all allocated resources in a batch. +// This only illustrates the fact that the config data are allocated with the allocator +// you pass to cli.AppRunner.init(allocator). +fn freeConfig() void { + allocator.free(config.ip); + if (config.arg2.len > 0) { + for (config.arg2) |item| { + allocator.free(item); + } + allocator.free(config.arg2); + } + if (gpa.deinit() == .leak) { + @panic("config leaked"); + } } fn run_sub3() !void {