-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Unexpected memory leak report using arena allocator #8312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Here: pub const ValueTree = struct {
arena: ArenaAllocator,
root: StringArray,
}; Shouldn't |
This is a slight footgun in how the arena currently works. You copied the state of the arena before the allocation: test "memory leak" {
var arena = ArenaAllocator.init(testing.allocator);
var tree = ValueTree{
.arena = arena, // state copy
.root = createArray(&arena.allocator, "foo"), // actual allocation
};
tree.arena.deinit();
} This is fixed by either copying after all the allocations have happened or removing the copy altogether. |
Ahh of course, I see it now. I guess at this stage fine to just close the issue then, not sure how this might be made less of a gotcha in the future. |
The footgun here will be fixed when #7769 is implemented, which will make the copy a compile error. |
In case it's a useful reference to anyone coming across this issue in the future, here's how I stopped shooting myself in the foot thanks to the explanations above: LewisGaul/zig-nestedtext@df2a675 |
It seems that ArenaAllocator instances should be [passed around as pointers](ziglang/zig#8312), not values. Otherwise the internal structs don't get copied around properly and thus leaks are introduced.
Excuse the length code snippet, but hopefully it's enlightening - I spent a long time narrowing down the problem from a real project!
In words: I'm getting an unexpected memory leak reported when using
std.testing.allocator
, seemingly coming from an allocation made by an arena allocator that I'm callingdeinit()
on.Even more strange, is that this seems to be sensitive to:
deinit()
Expected behaviour in the code below: no memory leaks.
Actual behaviour in the code below: memory leak for the first testcase only.
This is seen with fairly recent master branch, as well as on 0.7.1.
The text was updated successfully, but these errors were encountered: