Skip to content

Instantly share code, notes, and snippets.

@LewisGaul
Last active March 21, 2021 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LewisGaul/c0a2ff38607f55ab589700ada452f7c4 to your computer and use it in GitHub Desktop.
Save LewisGaul/c0a2ff38607f55ab589700ada452f7c4 to your computer and use it in GitHub Desktop.
Weird Zig bug with memory leak reporting?
const std = @import("std");
const testing = std.testing;
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const StringArray = std.ArrayList([]const u8);
pub const ValueTree = struct {
arena: ArenaAllocator,
root: StringArray,
};
fn createArray(allocator: *Allocator, input: []const u8) StringArray {
var array = StringArray.init(allocator);
array.append(input) catch unreachable;
return array;
}
test "memory leak" {
var arena = ArenaAllocator.init(testing.allocator);
var tree = ValueTree{
.arena = arena,
.root = createArray(&arena.allocator, "foo"),
};
tree.arena.deinit();
}
test "no memory leak - no function call in struct creation" {
var arena = ArenaAllocator.init(testing.allocator);
var in_array = createArray(&arena.allocator, "foo");
var tree = ValueTree{
.arena = arena,
.root = in_array,
};
tree.arena.deinit();
}
test "no memory leak - direct arena deinit" {
var arena = ArenaAllocator.init(testing.allocator);
var tree = ValueTree{
.arena = arena,
.root = createArray(&arena.allocator, "foo"),
};
arena.deinit();
}
23:53:24 LEGAUL-P12W1:zig-nestedtext/(main)$../zig/build/zig test test.zig
[gpa] (err): Memory leak detected:
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/heap/arena_allocator.zig:71:106: 0x20c7ef in std.heap.arena_allocator.ArenaAllocator.alloc (test)
var cur_node = if (self.state.buffer_list.first) |first_node| first_node else try self.createNode(0, n + ptr_align);
^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/mem/Allocator.zig:290:40: 0x23c6de in std.mem.Allocator.allocAdvancedWithRetAddr (test)
const byte_slice = try self.allocFn(self, byte_count, a, len_align, return_address);
^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/mem/Allocator.zig:273:62: 0x236eb6 in std.mem.Allocator.allocAdvancedWithRetAddr (test)
if (a == @alignOf(T)) return allocAdvancedWithRetAddr(self, T, null, n, exact, return_address);
^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/mem/Allocator.zig:371:45: 0x22a597 in std.mem.Allocator.reallocAdvancedWithRetAddr (test)
return self.allocAdvancedWithRetAddr(T, new_alignment, new_n, exact, return_address);
^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/mem/Allocator.zig:344:43: 0x22a4cb in std.mem.Allocator.reallocAtLeast (test)
return self.reallocAdvancedWithRetAddr(old_mem, old_alignment, new_n, .at_least, @returnAddress());
^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/array_list.zig:310:65: 0x22a223 in std.array_list.ArrayListAligned([]const u8,null).ensureCapacity (test)
const new_memory = try self.allocator.reallocAtLeast(self.allocatedSlice(), better_capacity);
^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/array_list.zig:325:36: 0x211f7f in std.array_list.ArrayListAligned([]const u8,null).addOne (test)
try self.ensureCapacity(newlen);
^
/mnt/c/Users/legaul/Documents/personal/zig/build/lib/zig/std/array_list.zig:182:49: 0x20acca in std.array_list.ArrayListAligned([]const u8,null).append (test)
const new_item_ptr = try self.addOne();
^
All 3 tests passed.
1 errors were logged.
1 tests leaked memory.
error: the following test command failed with exit code 1:
zig-cache/o/ec2b54238df6496fab97d01c7069088d/test /mnt/c/Users/legaul/Documents/personal/zig/build/zig
23:53:39 LEGAUL-P12W1:zig-nestedtext/(main)$../zig/build/zig version
0.8.0-dev.1478+48efa3bcb
@LewisGaul
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment