Regarding the alignment error, I’m not sure if it’s the fact that the keys are being freed while iterating and this messes up the iteration itself. If this is the case, I have no idea how to free the keys without being able to iterate over them. Maybe someone here can help us out with that. In the meantime, you could use an arena allocator and just free everything all at once at the end, so instead of your current gpa you could use
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
With this you can remove the freeing iteration code I recommended above and pass this allocator to Context.init.
Using an arena allocator fixed tha alignment error, but not the error that was the originial issue which is that the data was being corrupted. Hmm… I don’t know maybe one of the Zig core team will be able to shed better light on the issue because I’m stomped.
EDIT: it works fine with the iteration code when using the arena allocator. In fact when I remove the freeing iteration code a memory leak occurs.
This makes me suspect that somewhere there’s more than one allocator being used to separately allocate and free. I would recommend then that you replace all other allocators in your program with the single arena allocator, just to flush out any such allocator mismatch error. If later on you want to go back to other allocator types, you can replace each occurrence of the arena one at a time, testing each time whether it causes an issue.
I just tried that but the data in the StringHashMap still gets corrupted.
I think the keys are being stored OK. If you see in the output, the key would be add and it’s being printed out as add, so the key is OK. Now, the rule is composed of expressions which in turn contain strings. These strings are pulled from token values produced by the tokenizer, but they are not being allocated on the heap so if the tokenizer’s buffer for input is overwritten with the next input line, there go the bytes for those strings! You’ll have to find a way to allocate those strings that make up the expressions that make up the rules so they can persist beyond the input loop.
3 Likes