Can't free sentinel terminated many item pointer

I am trying to understand sentinel terminated many item pointers and am running into an issue when trying to free the memory allocated with allocSentinel.

The relevant code is

const allocator = std.heap.page_allocator;

var str: [*:0]u8 = try allocator.allocSentinel(u8, 100, 0);
defer allocator.free(str);

When I run this I get this error

error: type '[*:0]u8' does not support field access
if (slice.len == 0 and std.meta.sentinel(Slice) == null) return &[0]u8{};

The code otherwise compiles if I removed the free statement, but then it creates a memory leak. I have tried to read through documentation but have not been able to make sense of it.

defer allocator.free(std.mem.span(str));

free needs a slice, also take a look at:

1 Like

This was the trick, thank you so much. I will read the linked post later.

1 Like