#properly deinit std.StringHashMap

1 messages · Page 1 of 1 (latest)

pastel glade
#

Take the code below:

const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const alloc = gpa.allocator();
    defer _ = gpa.deinit();

    var map = std.StringHashMap(void).init(alloc);
    var somekey = try alloc.dupe(u8, "somekey");
    try map.put(somekey, {});

    var map_iter = map.keyIterator();
    while(map_iter.next()) |key| {
        alloc.destroy(key);
    }
    map.deinit();
}

When the key is being freed this gives me an "invalid free".
If I try alloc.free it says expected []T or *[_]T, passed *[]const u8
Am I being stupid here?

vague portal
#

hashmap iterators give you pointers to keys and values, so you'll need to deref them if you want the value; and you should be using free