reader.appendRemainingUnlimited memory leak

In my understanding output.toOwnedSlice(gpa) should be freeing output’s memory allocation. And the decomp reader doesn’t allocate anything internally. What am I missing here?

pub fn main(init: std.process.Init) !void {
    var _gpa = std.heap.DebugAllocator(.{ .stack_trace_frames = 100 }).init;

    const gpa = _gpa.allocator();

    // const gpa = init.gpa;

    // deflate
    const files = [3][]const u8{
        &embed.index_ov2640_html_gz,
        &embed.index_ov3660_html_gz,
        &embed.index_ov5640_html_gz,
    };
    for (files) |data| {
        const name = try gzipNameParser(gpa, data);
        defer gpa.free(name);
        std.log.info("{s}", .{name});
        const file = try std.Io.Dir.cwd().createFile(init.io, name, .{});
        defer file.close(init.io);

        var gzip_reader: std.Io.Reader = .fixed(data);

        var output = try std.ArrayList(u8).initCapacity(gpa, 1);
        defer output.deinit(gpa);

        const mem_window = try gpa.alloc(u8, std.compress.flate.max_window_len);
        defer gpa.free(mem_window);

        var decomp = std.compress.flate.Decompress.init(
            &gzip_reader,
            .gzip,
            mem_window,
        );

        try decomp.reader.appendRemainingUnlimited(gpa, &output);
        std.log.debug("Capacity of Output arraylist {d}", .{output.capacity});

        try file.writeStreamingAll(init.io, try output.toOwnedSlice(gpa));
        std.log.debug("Capacity of Output arraylist {d}", .{output.capacity});
    }

    _ = _gpa.detectLeaks();
}

Output:

error(DebugAllocator): memory address 0x7f9dce520000 leaked: 
/usr/lib/zig/std/Io/Writer.zig:2624:42: 0x121bbe3 in ensureTotalCapacity (std.zig)
        return ensureTotalCapacityPrecise(a, better_capacity);
                                         ^
/usr/lib/zig/std/Io/Writer.zig:2617:35: 0x121bb0e in ensureUnusedCapacity (std.zig)
        return ensureTotalCapacity(a, new_capacity);
                                  ^
/usr/lib/zig/std/Io/Writer.zig:2705:35: 0x121aef1 in drain (std.zig)
            a.ensureUnusedCapacity(bytes.len + splat_len + 1) catch return error.WriteFailed;
                                  ^
/usr/lib/zig/std/Io/Writer.zig:539:26: 0x10349df in write (std.zig)
    return w.vtable.drain(w, &.{bytes}, 1);
                         ^
/usr/lib/zig/std/Io/Reader.zig:172:30: 0x1034389 in stream (std.zig)
        const n = try w.write(buffer);
                             ^
/usr/lib/zig/std/Io/Reader.zig:261:27: 0x10e8e15 in streamRemaining (std.zig)
        offset += r.stream(w, .unlimited) catch |err| switch (err) {
                          ^
/usr/lib/zig/std/Io/Reader.zig:401:24: 0x11dd19b in appendRemainingUnlimited (std.zig)
    _ = streamRemaining(r, &a.writer) catch |err| switch (err) {
                       ^
/home/user/projects/pico-eye/main/web/gzip_games.zig:37:51: 0x11d2d7a in main (gzip_games.zig)
        try decomp.reader.appendRemainingUnlimited(gpa, &output);
                                                  ^
/usr/lib/zig/std/start.zig:737:30: 0x11d3ffe in callMain (std.zig)
    return wrapMain(root.main(.{
                             ^
/usr/lib/zig/std/start.zig:190:5: 0x11d20e1 in _start (std.zig)
    asm volatile (switch (native_arch) {
    ^

You didn’t defer free the slice that you now own.

I thought owned meant it was now on the stack lol. Yep this was the problem.

No it transfers ownership to the caller, but only after it shrinks the allocation to the actual size that is being used.

Which may free the original after moving the data to a new allocation. But it may also be able to resize in place to avoid the slower allocate-copy-free(old). That is what is happening in this case, which is why the leak points to outputs allocation instead of toOwnedSlice.

regardless, the allocation that you are not freeing is the result of toOwnedSlice