From 6b2ce7e4250719ac4b15158ec289bd3fcf645fb5 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 28 Apr 2022 00:28:07 -0300 Subject: [PATCH] properly fix memleaks --- src/lang.zig | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/lang.zig b/src/lang.zig index 98b1e77..5ded695 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -479,25 +479,24 @@ pub const CommandList = struct { pub fn deinit(self: *Self) void { for (self.list.items) |cmd_ptr| { - //self.list.allocator.destroy(cmd_ptr); inline for (@typeInfo(Command.Tag).Enum.fields) |field| { if (cmd_ptr.tag == @field(Command.Tag, field.name)) { const actual_tag = @field(Command.Tag, field.name); // if we find a match on the tag, we can get the type const typ = Command.tagToType(actual_tag); - _ = typ; - // TODO fix - - //inline for (@typeInfo(typ).Struct.fields) |cmd_field| { - // switch (cmd_field.field_type) { - // []u8, []const u8 => self.list.allocator.destroy(@field(typ, cmd_field.name)), - // else => {}, - // } - //} + const inner_command = cmd_ptr.cast(typ).?; + inline for (@typeInfo(typ).Struct.fields) |cmd_field| { + switch (cmd_field.field_type) { + []u8, []const u8 => self.list.allocator.free(@field(inner_command, cmd_field.name)), + else => {}, + } + } } } + + self.list.allocator.destroy(cmd_ptr); } self.list.deinit(); }