Hey all, new to Zig and attempting to port a few of my other projects to the language. I’m running in to an issue while attempting to use an AutoHashMap as a member on a struct. Here is the code:
const NodeType = enum { Root, Inner, Leaf };
const Node = struct {
allocator: std.mem.Allocator,
node_type: NodeType,
key_map: *std.AutoHashMap(i32, []u8),
child_node: ?*Node,
pub fn init(allocator: std.mem.Allocator, node_type: NodeType) @This() {
var key_map = std.AutoHashMap(i32, []u8).init(allocator);
const map = &key_map;
return Node{ .allocator = allocator, .node_type = node_type, .key_map = map, .child_node = null };
}
pub fn deinit(self: @This()) void {
var it = self.key_map.valueIterator();
while (it.next()) |value_ptr| {
self.allocator.free(value_ptr.*);
}
}
pub fn get(self: @This(), key: i32) ?[]u8 {
return self.key_map.*.get(key);
}
pub fn insert(self: @This(), key: i32, value: []u8) !void {
try self.key_map.put(key, value);
}
};
Using the following test:
test "basic node test" {
const allocator = std.testing.allocator;
const node = Node.init(allocator, NodeType.Root);
defer node.deinit();
const data = try allocator.alloc(u8, DEFAULT_PAGE_SIZE);
@memset(data, 10);
try node.insert(0, data);
}
While running this I’m seeing the following:
thread 7643268 panic: reached unreachable code
/Users/devan/.zvm/0.17.0/lib/std/debug.zig:442:14: 0x1000969e3 in assert (test)
if (!ok) unreachable; // assertion failure
^
/Users/devan/.zvm/0.17.0/lib/std/debug.zig:1870:15: 0x1000bc637 in lock (test)
assert(l.state == .unlocked);
^
/Users/devan/.zvm/0.17.0/lib/std/hash_map.zig:1116:44: 0x1001c7e0f in getOrPutContextAdapted__func_999 (test)
self.pointer_stability.lock();
^
/Users/devan/.zvm/0.17.0/lib/std/hash_map.zig:1103:56: 0x1001c7b93 in getOrPutContext (test)
const gop = try self.getOrPutContextAdapted(allocator, key, ctx, ctx);
^
/Users/devan/.zvm/0.17.0/lib/std/hash_map.zig:1029:52: 0x1001c799f in putContext (test)
const result = try self.getOrPutContext(allocator, key, ctx);
^
/Users/devan/.zvm/0.17.0/lib/std/hash_map.zig:324:45: 0x1001c78d3 in put (test)
return self.unmanaged.putContext(self.allocator, key, value, self.ctx);
^
/Users/devan/Documents/Projects/zig-tree/src/root.zig:35:29: 0x1001c7403 in insert (test)
try self.key_map.put(key, value);
^
/Users/devan/Documents/Projects/zig-tree/src/root.zig:68:20: 0x1001c75db in test.basic btree test (test)
try node.insert(0, data);
^
/Users/devan/.zvm/0.17.0/lib/compiler/test_runner.zig:295:25: 0x1001a31c3 in mainTerminal (test)
if (test_fn.func()) |_| {
^
/Users/devan/.zvm/0.17.0/lib/compiler/test_runner.zig:74:28: 0x1001a2ca3 in main (test)
return mainTerminal(init);
^
/Users/devan/.zvm/0.17.0/lib/std/start.zig:789:88: 0x1001a0bd3 in callMain (test)
if (fn_info.param_types[0].? == std.process.Init.Minimal) return wrapMain(root.main(.{
^
???:?:?: 0x19cf09d53 in start (/usr/lib/dyld)
error: the following test command terminated with signal ABRT:
.zig-cache/o/d3dbf24e49389327f0244c523c2762b1/test --seed=0x9391a1ee
I’m wondering what I’m doing wrong? Is it idiomatic to put pointers as field types in structs or can that lead to problems?