mirror of
https://github.com/ziglang/zig.git
synced 2026-02-20 08:14:48 +00:00
Fix memory leak in parser tests
The `arena` instance being used bythe parse tree was valid and
pointed to valid memory, but existed as a local variable inside the
stack frame of the `parse` function (the `const arena`), which was never
stored anywhere before leaving the scope.
This meant that code above the `parse` function saw a valid instance of
an `ArenaAllocator` that pointed to the same backing memory, but didn't
posess any of the local state built up after the call to `parseRoot`,
basically the caller saw an empty arena.
This meant that when `deinit` was called, it saw an Arena with 0
allocations in it's `buffer_list` and wasn't able to destroy any of the
memory. This caused it to leak and caused FailingAllocator to balk.
The fix is to make sure the parse tree is using the same instance of
ArenaAllocator as is reported up the call stack, the one inside the
`Tree{}` object. I'm not sure why that field is marked with a comment
to remove it, as it's used by the `std.ast.Tree.deinit()` function, but
this change seems to solve the problem.
This commit is contained in:
parent
4e28c2571d
commit
16aee1f58a
@ -31,10 +31,10 @@ pub fn parse(allocator: *Allocator, source: []const u8) !Tree {
|
||||
.tokens = token_list,
|
||||
.errors = Tree.ErrorList.init(arena),
|
||||
// TODO: Remove (not used/needed anywhere)
|
||||
.arena_allocator = undefined,
|
||||
.arena_allocator = tree_arena,
|
||||
};
|
||||
|
||||
tree.root_node = try parseRoot(arena, &it, &tree);
|
||||
tree.root_node = try parseRoot(&tree.arena_allocator.allocator, &it, &tree);
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user