When I try to return an ArrayList:
const std = @import("std");
const Allocator = std.mem.Allocator;
pub fn main() !void {}
fn notReturn(allocator: Allocator) !void {
var list = std.ArrayList(i32).init(allocator);
defer list.deinit();
try list.append(1);
try list.append(2);
std.debug.print("List: {any}\n", .{list.items});
}
fn isReturn(allocator: Allocator) !std.ArrayList(i32) {
var list = std.ArrayList(i32).init(allocator);
defer list.deinit();
try list.append(1);
try list.append(2);
std.debug.print("List: {any}\n", .{list.items});
return list;
}
test "not return test" {
try notReturn(std.heap.page_allocator);
}
test "return test" {
const result = try isReturn(std.heap.page_allocator);
std.debug.print("Return: {any}\n", .{result});
}
It compiles succesfully but when run it gives error:
zig build test
Test [1/2] test.not return test... List: { 1, 2 }
Test [2/2] test.return test... List: { 1, 2 }
Return: array_list.ArrayListAligned(i32,null){ .items = { Segmentation fault at address 0x
7feff8962000
/media/lucifer/DATA/IMPORTANT/zig-linux-x86_64-0.10.1/lib/std/fmt.zig:661:22: 0x21c18d in
formatType__anon_4934 (test)
for (value) |elem, i| {
^
/media/lucifer/DATA/IMPORTANT/zig-linux-x86_64-0.10.1/lib/std/fmt.zig:595:87: 0x2187c2 in
formatType__anon_4131 (test)
try formatType(@field(value, f.name), ANY, options, writer, max_depth - 1)
;
^
/media/lucifer/DATA/IMPORTANT/zig-linux-x86_64-0.10.1/lib/std/fmt.zig:188:41: 0x218426 in
format__anon_4119 (test)
.alignment = placeholder.alignment,
^
/media/lucifer/DATA/IMPORTANT/zig-linux-x86_64-0.10.1/lib/std/io/writer.zig:28:34: 0x215a7
0 in print__anon_3276 (test)
return std.fmt.format(self, format, args);
^
/media/lucifer/DATA/IMPORTANT/zig-linux-x86_64-0.10.1/lib/std/debug.zig:93:27: 0x213b2c in
print__anon_1477 (test)
nosuspend stderr.print(fmt, args) catch return;
^
/home/lucifer/test/src/main.zig:40:20: 0x213a98 in test.return test (test)
std.debug.print("Return: {any}\n", .{result});
^
/media/lucifer/DATA/IMPORTANT/zig-linux-x86_64-0.10.1/lib/test_runner.zig:63:28: 0x2168f3
in main (test)
} else test_fn.func();
^
/media/lucifer/DATA/IMPORTANT/zig-linux-x86_64-0.10.1/lib/std/start.zig:604:22: 0x21447c i
n posixCallMainAndExit (test)
root.main();
^
/media/lucifer/DATA/IMPORTANT/zig-linux-x86_64-0.10.1/lib/std/start.zig:376:5: 0x213f81 in
_start (test)
@call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
^
error: the following test command crashed:
/home/lucifer/test/zig-cache/o/4b94fda1f98d2f61fa2a145e0b04a6bd/test
error: test...
error: The following command exited with error code 1:
/media/lucifer/DATA/IMPORTANT/zig-linux-x86_64-0.10.1/zig test /home/lucifer/test/src/main
.zig --cache-dir /home/lucifer/test/zig-cache --global-cache-dir /home/lucifer/.cache/zig
--name test --enable-cache
error: the following build command failed with exit code 1:
/home/lucifer/test/zig-cache/o/8ab9fd52cdc07f23f15ce435a5b2c0ba/build /media/lucifer/DATA/
IMPORTANT/zig-linux-x86_64-0.10.1/zig /home/lucifer/test /home/lucifer/test/zig-cache /hom
e/lucifer/.cache/zig test
I think this is because the ArrayList is freed after exiting the function. How to fix it? My compiler version is 0.10.1.