# Why is memory still leaked after calling \`.toOwnedSlice\` on an ArrayList

**URL:** <https://ziggit.dev/t/why-is-memory-still-leaked-after-calling-toownedslice-on-an-arraylist/7613>\
**Category:** Help\
**Tags:** memory-management\
**Created:** [January 1, 2025, 12:20am UTC](https://ziggit.dev/t/why-is-memory-still-leaked-after-calling-toownedslice-on-an-arraylist/7613 "2025-01-01T00:20:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scubbo](https://ziggit.dev/user_avatar/ziggit.dev/scubbo/32/4058_2.png) [@scubbo](https://ziggit.dev/u/scubbo)\
**Post date:** [January 1, 2025, 12:20am UTC](https://ziggit.dev/t/why-is-memory-still-leaked-after-calling-toownedslice-on-an-arraylist/7613/1 "2025-01-01T00:20:06Z")

</div>

The docs for `.toOwnedSlice` [read](https://github.com/ziglang/zig/blob/master/lib/std/array_list.zig#L108-L109) “_The caller owns the returned memory. Empties this ArrayList, Its capacity is cleared, making deinit() safe but unnecessary to call._”. However, running this test:

```zig
const std = @import("std");
const print = std.debug.print;

test ".toOwnedSlice does not seem to make deinit unnecessary" {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var listOfLists = std.ArrayList([]u32).init(allocator);
    try listOfLists.append(try buildAList(0, allocator));
    try listOfLists.append(try buildAList(1, allocator));

    print("{any}\n", .{listOfLists.toOwnedSlice()});
}

fn buildAList(val: u32, allocator: std.mem.Allocator) ![]u32 {
    var list = std.ArrayList(u32).init(allocator);

    try list.append(val);

    return list.toOwnedSlice();
}

```

shows three memory leaks - two at `try list.append(val);`, and one at `try listOfLists.append(try buildAList(0, allocator));`, contrary to my expectations.

Unfortunately I don’t have enough Zig terminology to articulate my question any better than this, but hopefully my confusion is clear. What’s going on here that I’m misunderstanding?

---

<div class="post-metadata">

**Author:** ![scubbo](https://ziggit.dev/user_avatar/ziggit.dev/scubbo/32/4058_2.png) [@scubbo](https://ziggit.dev/u/scubbo)\
**Post date:** [January 1, 2025, 12:28am UTC](https://ziggit.dev/t/why-is-memory-still-leaked-after-calling-toownedslice-on-an-arraylist/7613/2 "2025-01-01T00:28:07Z")

</div>

Hmnm - _this_ has no memory leaks:

```zig
const std = @import("std");
const print = std.debug.print;

test ".toOwnedSlice does not seem to make deinit unnecessary" {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var list_of_lists = std.ArrayList([]u32).init(allocator);
    try list_of_lists.append(try buildAList(0, allocator));
    try list_of_lists.append(try buildAList(1, allocator));

    const outer_slice = try list_of_lists.toOwnedSlice();
    print("{any}\n", .{outer_slice});
    for (outer_slice) |inner_slice| {
        allocator.free(inner_slice);
    }
    allocator.free(outer_slice);
}

fn buildAList(val: u32, allocator: std.mem.Allocator) ![]u32 {
    var list = std.ArrayList(u32).init(allocator);

    try list.append(val);

    return list.toOwnedSlice();
}

```

So, is it accurate to say that `.toOwnedSlice()` means that you don’t need to call `.deinit()` _on the ArrayList_, but you still need to call `allocator.free` on the resultant slice?

---

<div class="post-metadata">

**Author:** ![andrewrk](https://ziggit.dev/user_avatar/ziggit.dev/andrewrk/32/7011_2.png) [@andrewrk](https://ziggit.dev/u/andrewrk)\
**Post date:** [January 1, 2025, 12:34am UTC](https://ziggit.dev/t/why-is-memory-still-leaked-after-calling-toownedslice-on-an-arraylist/7613/3 "2025-01-01T00:34:55Z")

</div>

> [@scubbo](#):
>
> you still need to call `allocator.free` on the resultant slice

Yes - that is precisely what “caller owns the returned memory” means.

---

<div class="post-metadata">

**Author:** ![scubbo](https://ziggit.dev/user_avatar/ziggit.dev/scubbo/32/4058_2.png) [@scubbo](https://ziggit.dev/u/scubbo)\
**Post date:** [January 1, 2025, 1:06am UTC](https://ziggit.dev/t/why-is-memory-still-leaked-after-calling-toownedslice-on-an-arraylist/7613/4 "2025-01-01T01:06:40Z")

</div>

Got it - thanks!
