Deiniting allocator passed to a function

Yeah, this concept tripped me up the first time I encountered it too. The short answer is you need to free the slice you returned here:

const got = try findAnagrams(testing.allocator, tc.input1, tc.input2);
defer testing.allocator.free(got);

The reason why is when you call toOwnedSlice, you are now responsible for cleaning up the memory instead of using ArrayList’s deinit. A more detailed explanation can be found here.

4 Likes