Skip to content

Memory leak when using fs.readAllAlloc multiple times #4656

@leroycep

Description

@leroycep

Not sure if this is a bug in the LeakCountAllocator, or readAllAlloc, but the following code will leak an allocation:

test "readAllAlloc memory leak" {
    const cwd = fs.cwd();
    try cwd.writeFile("test.txt", "Hello, world!\n");
    const file = try cwd.openFile("test.txt", .{ .read = true });

    const file_contents = try file.inStream().stream.readAllAlloc(testing.allocator, 50);
    testing.allocator.free(file_contents);

    const file_contents2 = try file.inStream().stream.readAllAlloc(testing.allocator, 50);
    testing.allocator.free(file_contents2);
}

The following output is generated when using zig test:

test "readAllAlloc memory leak"...error - detected leaked allocations without matching free: 1

/home/leroycep/sources/github.com/ziglang/zig/lib/std/special/test_runner.zig:52:46: 0x243b78 in std.special.main (test)
                error.Leak => std.debug.panic("", .{}),
                                             ^
/home/leroycep/sources/github.com/ziglang/zig/lib/std/start.zig:256:37: 0x20a36b in std.start.posixCallMainAndExit (test)
            const result = root.main() catch |err| {
                                    ^
/home/leroycep/sources/github.com/ziglang/zig/lib/std/start.zig:120:5: 0x20a19f in std.start._start (test)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^

Edit: I should also mention that if the second readAllAlloc is removed, no memory leak is detected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions