Hello, i'm building a small compiler and i came to the part when i need to use an arraylist. But the thing is that i keep leaking memory and i don't understand why.
error(gpa): memory address leaked:
lib/std/array_list.zig:474:67: in ensureTotalCapacityPrecise (oneth)
const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity);
lib/std/array_list.zig:450:51: in ensureTotalCapacity (oneth)
return self.ensureTotalCapacityPrecise(better_capacity);
lib/std/array_list.zig:500:41: in addOne (oneth)
try self.ensureTotalCapacity(newlen);
lib/std/array_list.zig:261:49: in append (oneth)
const new_item_ptr = try self.addOne();
oneth/src/codegen.zig:54:22: in genInt (oneth)
try c.code.append(opcode);
oneth/src/codegen.zig:41:44: 0x10f0082 in gen (oneth)
.number_literal => try c.genInt(),
The code
pub fn init(gpa: std.mem.Allocator, source: [:0]const u8, tokens: TokenList.Slice) CodeGen {
return .{
.string_map = .init(gpa),
.token_tags = tokens.items(.tag),
.token_locs = tokens.items(.loc),
.source = source,
.code = .init(gpa),
.gpa = gpa,
};
}
pub fn gen(c: *CodeGen) ![]const u8 {
while (c.tp < c.token_tags.len) : (c.tp += 1) {
switch (c.token_tags[c.tp]) {
.number_literal => try c.genInt(),
else => {},
}
}
return c.code.toOwnedSlice();
}
fn genInt(c: *CodeGen) !void {
const start = c.token_locs[c.tp].start;
const end = c.token_locs[c.tp].stop;
const opcode: u8 = @intFromEnum(OpCode.int);
try c.code.append(opcode);
std.log.info("int: `{s}`\n", .{ c.source[start..end] });
const int = try std.fmt.parseInt(usize, c.source[start..end], 10);
const bytes = std.mem.toBytes(int);
try c.code.appendSlice(&bytes);
}
I'm using the general purpose allocator in an arraylist of u8.
If you have an idea, i might just have skip something