#mem leak help

1 messages · Page 1 of 1 (latest)

small nacelle
#

Im failing to identify where / how the mem leak is occurring. const print = std.debug.print;
T is known to the wrapping fn.

pub fn init(alloc: Allocator) []u8 {
    const ptr: *T = alloc.create(T) catch unreachable; //TODO: handle better
    ptr.* = T.init(alloc);
    var bytes: []u8 = std.mem.asBytes(ptr);
    print("\ndynamic_init --> {s}\t{}\t{}\n", .{ name, @intFromPtr(ptr), @intFromPtr(bytes.ptr) });
    return bytes;
}

struct

pub const Name = struct {
    value: std.ArrayList(u8),

    pub fn init(alloc: Allocator) Name {
        const self = Name{
            .value = std.ArrayList(u8).init(alloc),
        };
        print("\nName.init() {}\n", .{@intFromPtr(&self)});
        return self;
    }
    pub fn deinit(self: *Name) void {
        print("\nName.deinit() {}\n", .{@intFromPtr(self)});
        self.value.deinit();
    }
};

log

Name.init() 49878658664

dynamic_init --> testing_structs.Name   1150146838528   1150146838528

Name.deinit() 1150146838528
[gpa] (err): memory address 0x10bca180000 leaked:
E:\projects\zig\exporation\src\zecs\TypeManager.zig:56:49: 0x7ff7b76ee3e6 in init (test.exe.obj)
                    const ptr: *T = alloc.create(T) catch unreachable; //TODO: handle better
random laurel
#

this code makes almost zero sense, but in general: in deinit you only free the .value arraylist, you also need to free self, which you allocated with create

small nacelle
#

I can see where you are coming from. its a fair amount of code to share it all, but this weekend ill throw it on github. Ill give your suggestion a shot.

glass plover
#

In this particular case, I would just not create the Name.

#
fn init(ally: Allocator) Self {
    return .{
        .value = std.ArrayList(u8).init(ally),
    };
}