chameleon icon indicating copy to clipboard operation
chameleon copied to clipboard

Memory Leak?

Open nmoagit opened this issue 9 months ago • 2 comments

Disclaimer: I don't know what I am doing. I am trying to learn Zig and I noticed this and I was wondering if you could help explain what is going on.

EDIT: I can stop the leak by freeing the allocation inside of the loop. Is this the correct thing to do?

Zig: zig-linux-x86_64-0.14.0-dev

trying chameleon, /magurotuna/zig-deque, std.io.getStdOut

// setup: 
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

var deque = try Deque(usize).init(allocator);
defer deque.deinit();
   
try deque.pushBack(1);
try deque.pushBack(2);
try deque.pushFront(42);

const writer = std.io.getStdOut().writer();
var c = Chameleon.initRuntime(.{ .allocator = allocator });
   
var it = deque.iterator();

then when trying to print values using the iterator

// NO MEMORY LEAK
 while (it.next()) |val| {
    try writer.print("{d} \n", .{val.*});
}

^-- No memory leak

// MEMORY LEAK
 while (it.next()) |val| {
    try writer.print("{s} \n", .{try c.green().bold().fmt("{d}", .{val.*})});
}

^-- memory leak

// FIXED? MEMORY LEAK
 while (it.next()) |val| {
    const fmt_text = try c.green().bold().fmt("{d}", .{val.*});
    try writer.print("{s} \n", .{});
    defer allocator.free(fmt_text);
}

^-- FIXED? memory leak. Is this correct?

and this is the leak message:

error(gpa): memory address 0x7f1f4ab20000 leaked:
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/mem.zig:3317:36: 0x1043d50 in concatMaybeSentinel__anon_3572 (deque_test)
    const buf = try allocator.alloc(T, total_len);
                                   ^
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/mem.zig:3291:31: 0x103d588 in concat__anon_2508 (deque_test)
    return concatMaybeSentinel(allocator, T, slices, null);
                              ^
/.cache/zig/p/12208314a762057bf5ed82b26b02cbfcf426066b9920bfdd1ddcb68d1d0c55c45ce3/src/api/Runtime.zig:25:30: 0x103d1a2 in fmt__anon_2476 (deque_test)
        return std.mem.concat(self.allocator, u8, &.{ self.open.items, formatted, self.close.items });
                             ^
../src/main.zig:34:62: 0x103dc48 in main (deque_test)
        try writer.print("{s} \n", .{try c.green().bold().fmt("{d}", .{val.*})});
                                                             ^
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/start.zig:656:37: 0x103ceda in posixCallMainAndExit (deque_test)
            const result = root.main() catch |err| {
                                    ^
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/start.zig:271:5: 0x103ca8d in _start (deque_test)
    asm volatile (switch (native_arch) {
    ^

error(gpa): memory address 0x7f1f4ab20020 leaked:
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/mem.zig:3317:36: 0x1043d50 in concatMaybeSentinel__anon_3572 (deque_test)
    const buf = try allocator.alloc(T, total_len);
                                   ^
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/mem.zig:3291:31: 0x103d588 in concat__anon_2508 (deque_test)
    return concatMaybeSentinel(allocator, T, slices, null);
                              ^
/.cache/zig/p/12208314a762057bf5ed82b26b02cbfcf426066b9920bfdd1ddcb68d1d0c55c45ce3/src/api/Runtime.zig:25:30: 0x103d1a2 in fmt__anon_2476 (deque_test)
        return std.mem.concat(self.allocator, u8, &.{ self.open.items, formatted, self.close.items });
                             ^
../src/main.zig:34:62: 0x103dc48 in main (deque_test)
        try writer.print("{s} \n", .{try c.green().bold().fmt("{d}", .{val.*})});
                                                             ^
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/start.zig:656:37: 0x103ceda in posixCallMainAndExit (deque_test)
            const result = root.main() catch |err| {
                                    ^
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/start.zig:271:5: 0x103ca8d in _start (deque_test)
    asm volatile (switch (native_arch) {
    ^

error(gpa): memory address 0x7f1f4ab20040 leaked:
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/mem.zig:3317:36: 0x1043d50 in concatMaybeSentinel__anon_3572 (deque_test)
    const buf = try allocator.alloc(T, total_len);
                                   ^
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/mem.zig:3291:31: 0x103d588 in concat__anon_2508 (deque_test)
    return concatMaybeSentinel(allocator, T, slices, null);
                              ^
/.cache/zig/p/12208314a762057bf5ed82b26b02cbfcf426066b9920bfdd1ddcb68d1d0c55c45ce3/src/api/Runtime.zig:25:30: 0x103d1a2 in fmt__anon_2476 (deque_test)
        return std.mem.concat(self.allocator, u8, &.{ self.open.items, formatted, self.close.items });
                             ^
../src/main.zig:34:62: 0x103dc48 in main (deque_test)
        try writer.print("{s} \n", .{try c.green().bold().fmt("{d}", .{val.*})});
                                                             ^
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/start.zig:656:37: 0x103ceda in posixCallMainAndExit (deque_test)
            const result = root.main() catch |err| {
                                    ^
/zig/zig-linux-x86_64-0.14.0-dev.3286+05d8b565a/lib/std/start.zig:271:5: 0x103ca8d in _start (deque_test)
    asm volatile (switch (native_arch) {
    ^

nmoagit avatar Feb 27 '25 10:02 nmoagit

I see you haven't called c.deinit() anywhere in the above code which is the reason it leaks the memory. Report back if it does resolve the issue!

tr1ckydev avatar Mar 25 '25 07:03 tr1ckydev

@nmoagit, I believe that the fix you mentioned is the correct solution here.

evaneliasyoung avatar Aug 09 '25 20:08 evaneliasyoung