Let’s say need a temporary memory block in my function, which I allocate dynamically. Naturally, I would like it to be freed, should my function return an error - this is what errdefer
is for.
But I also want to free this block before my function ends, and the function MAY return error after that, which would trigger errdefer
.
Here’s my test program:
const std = @import("std");
const MyError = error { AnError };
pub fn main() void
{
ham() catch { std.debug.print("Error\n", .{}); };
}
fn ham() !void
{
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc8r = gpa.allocator();
var sl = try alloc8r.alloc(u8, 12);
errdefer alloc8r.free(sl);
alloc8r.free(sl);
return MyError.AnError;
}
Naturaly, it coredumps at errdefer alloc8r.free(sl);
. If i comment it out, it works as expected.
So my solution is to
errdefer if (sl.len > 0) alloc8r.free(sl);
And
alloc8r.free(sl);
sl.len = 0;
But I wonder if there are better ways. Are there?