I just started learning Zig allocations, and not understanding what the runtime is telling me. I get a memory leak but the code should deallocate the allocator, and free the char_buffer when leaving main?
If I am not using allocation correctly when converting between pointers, what are rules of thumb to follow when doing so from memory allocation perspective?
Basically you are allocating twice here. If you remove the first allocation (which is currently discarded by overwriting it with the dupe result) it should work.
Oh… I was confused by Copies m to newly allocated memory. Caller owns the memory I thought it applied to char_buf which was rightfully managed by me: new_bufischar_buf
If you just check the pointer addresses both times, you’ll see that they’re different. What’s happening is that you’re overwriting the first char_buf address with a new address from the dupe call.
Yes, I incorrectly thought that the char_buf pointer did not change, and contents dupenew_buf will be assigned to the char_buf pointer ,and so then allocator.free(char_buf) will free the memory. But I see now that I have created a dangling memory with the first allocation, and that’s what the gpa was complaining about.