# Confusion about releasing a resource from a future

**URL:** <https://ziggit.dev/t/confusion-about-releasing-a-resource-from-a-future/17584>\
**Category:** Help\
**Tags:** io, async\
**Created:** [September 12, 2026, 6:53pm UTC](https://ziggit.dev/t/confusion-about-releasing-a-resource-from-a-future/17584 "2026-09-12T18:53:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tropid](https://ziggit.dev/letter_avatar_proxy/v4/letter/t/f14d63/32.png) [@Tropid](https://ziggit.dev/u/Tropid)\
**Post date:** [September 12, 2026, 6:53pm UTC](https://ziggit.dev/t/confusion-about-releasing-a-resource-from-a-future/17584/1 "2026-09-12T18:53:58Z")

</div>

I’m trying to understand how to correctly manage resources when using futures. The following example does not compile:

```zig
const std = @import("std");
const Io = std.Io;
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;

pub fn main(init: std.process.Init) !void {
    const gpa = init.gpa;
    const io = init.io;

    var numbers_future = io.async(createNumbers, .{gpa});
    defer if (numbers_future.cancel(io)) |numbers| numbers.deinit(gpa) else |_| {};

    const numbers = try numbers_future.await(io);
    for (numbers.items) |number| {
        std.debug.print("Number: {}\n", .{number});
    }
}

fn createNumbers(gpa: Allocator) !std.ArrayList(i32) {
    var numbers: std.ArrayList(i32) = .empty;
    errdefer numbers.deinit(gpa);

    try numbers.append(gpa, 1);
    try numbers.append(gpa, 2);

    return numbers;
}

```

```zig
src/main.zig:11:59: error: expected type '*T', found '*const T'
    defer if (numbers_future.cancel(io)) |numbers| numbers.deinit(gpa) else |_| {};
                                                   ~~~~~~~^~~~~~~
src/main.zig:11:59: note: T = array_list.Aligned(i32,null)
src/main.zig:11:59: note: cast discards const qualifier

```

If I understand the Zig 0.16.0 changelog correctly this is the recommended pattern to handle resources.

> Use this pattern to avoid resource leaks and handle [Cancelation](https://ziglang.org/download/0.16.0/release-notes.html#Cancelation) gracefully:

> **[Future - 0.16.0 Release Notes ⚡ The Zig Programming Language](https://ziglang.org/download/0.16.0/release-notes.html#Future)**
>
> Download & Documentation

But it fails because the `std.ArrayList.deinit` function takes the `ArrayList` by a mutable pointer which seems to be very common in Zig for `deinit` functions.

Am I missing something? How to correctly release resources in this case?

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [September 12, 2026, 7:14pm UTC](https://ziggit.dev/t/confusion-about-releasing-a-resource-from-a-future/17584/2 "2026-09-12T19:14:40Z")

</div>

returned temporaries, even if captured in this way, are const. you must open a block, assign to a variable and then deinit. assuming the object is an ArrayList, this is safe to do, since the reason the function accepts `*T` is to do `list.* = undefined`, which is desirable in general to help catch use after free.

---

<div class="post-metadata">

**Author:** ![Tropid](https://ziggit.dev/letter_avatar_proxy/v4/letter/t/f14d63/32.png) [@Tropid](https://ziggit.dev/u/Tropid)\
**Post date:** [September 12, 2026, 7:18pm UTC](https://ziggit.dev/t/confusion-about-releasing-a-resource-from-a-future/17584/4 "2026-09-12T19:18:39Z")

</div>

That makes a lot of sense. Thanks!

For future reference, this code works:

```zig
    var numbers_future = io.async(createNumbers, .{gpa});
    defer if (numbers_future.cancel(io)) |numbers| {
        var n = numbers;
        n.deinit(gpa);
    } else |_| {};

```

---

<div class="post-metadata">

**Author:** ![ScottRedig](https://ziggit.dev/user_avatar/ziggit.dev/scottredig/32/5880_2.png) [@ScottRedig](https://ziggit.dev/u/ScottRedig)\
**Post date:** [September 12, 2026, 7:30pm UTC](https://ziggit.dev/t/confusion-about-releasing-a-resource-from-a-future/17584/5 "2026-09-12T19:30:46Z")

</div>

Temporary var is a good solution in general. In this case however, I’d probably just use toOwnedSlice() in the return from create numbers, and just have the slice in main, which doesn’t have this problem. In general prefer slices over arraylists if I’m done adding or removing values from it.

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [September 13, 2026, 12:50am UTC](https://ziggit.dev/t/confusion-about-releasing-a-resource-from-a-future/17584/6 "2026-09-13T00:50:46Z")

</div>

arenas leak like crazy with growing data structures such as array lists. Making code much less memory efficient just to avoid a block and `var` is just silly.
