# reader.appendRemainingUnlimited memory leak

**URL:** <https://ziggit.dev/t/reader-appendremainingunlimited-memory-leak/17953>\
**Category:** Help\
**Tags:** memory-management\
**Created:** [October 14, 2026, 3:53am UTC](https://ziggit.dev/t/reader-appendremainingunlimited-memory-leak/17953 "2026-10-14T03:53:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheDarkUndoing](https://ziggit.dev/user_avatar/ziggit.dev/thedarkundoing/32/9617_2.png) [@TheDarkUndoing](https://ziggit.dev/u/TheDarkUndoing)\
**Post date:** [October 14, 2026, 3:53am UTC](https://ziggit.dev/t/reader-appendremainingunlimited-memory-leak/17953/1 "2026-10-14T03:53:11Z")

</div>

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?

```zig
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:

```zig
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) {
    ^

```

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [October 14, 2026, 3:56am UTC](https://ziggit.dev/t/reader-appendremainingunlimited-memory-leak/17953/2 "2026-10-14T03:56:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TheDarkUndoing](https://ziggit.dev/user_avatar/ziggit.dev/thedarkundoing/32/9617_2.png) [@TheDarkUndoing](https://ziggit.dev/u/TheDarkUndoing)\
**Post date:** [October 14, 2026, 3:58am UTC](https://ziggit.dev/t/reader-appendremainingunlimited-memory-leak/17953/3 "2026-10-14T03:58:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [October 14, 2026, 4:02am UTC](https://ziggit.dev/t/reader-appendremainingunlimited-memory-leak/17953/4 "2026-10-14T04:02:27Z")

</div>

> [@TheDarkUndoing](#):
>
> In my understanding `output.toOwnedSlice(gpa)` should be freeing `output`’s memory allocation

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 `output`s allocation instead of `toOwnedSlice`.

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