#Help to understand the memory leak

1 messages · Page 1 of 1 (latest)

quaint canopy
#
const std = @import("std");
test {
    const alloc = std.testing.allocator;
    var a = std.ArrayList(u8).init(alloc);
    defer a.deinit();
    var b = std.ArrayList(u8).init(alloc);
    defer b.deinit();

    try a.appendSlice("Hello");
    try b.appendSlice("Zig");
    try a.appendSlice(try b.toOwnedSlice());
}

Can someone help me understand the mistake in my code?

woven delta
#

toOwnedSlice as the name implies takes ownership of the underlying slice which means b.deinit() does nothing and the allocation is leaked

quaint canopy
woven delta
#

appendSlice copies the slice passed in

quaint canopy
#

oh boy