How to fix memory leaks reported in core by the tracking allocator?

I’ve been fixing memory leaks in a couple of projects of mine, using the memory tracking allocator as described in chapter 13 of Karl Zylinski’s Understanding the Odin Programming Language (by the way, I have used Odin for two years, for hobby projects, and buying this book was totally worth, especially the chapter on manual memory management).

Well, I fixed all of the leaks reported in my code, mostly caused by string operations. My problem is some leaks are still reported, but only their origin in the core lib is mentioned:

 1 leaks:
/opt/Odin-dev-2025-01/core/strings/builder.odin(171:11) leaked 185 bytes
No bad `free`
15 leaks:
/opt/Odin-dev-2025-01/core/strings/builder.odin(171:11) leaked 184 bytes
/opt/Odin-dev-2025-01/core/strings/conversion.odin(106:2) leaked 1 bytes
/opt/Odin-dev-2025-01/core/strings/conversion.odin(106:2) leaked 2 bytes
/opt/Odin-dev-2025-01/core/strings/conversion.odin(106:2) leaked 3 bytes
/opt/Odin-dev-2025-01/core/strings/conversion.odin(106:2) leaked 7 bytes
/opt/Odin-dev-2025-01/core/strings/conversion.odin(141:2) leaked 1 bytes
/opt/Odin-dev-2025-01/core/strings/strings.odin(3037:10) leaked 14 bytes
/opt/Odin-dev-2025-01/core/strings/strings.odin(3037:10) leaked 2 bytes
/opt/Odin-dev-2025-01/core/strings/strings.odin(3037:10) leaked 2 bytes
/opt/Odin-dev-2025-01/core/strings/strings.odin(3037:10) leaked 2 bytes
/opt/Odin-dev-2025-01/core/strings/strings.odin(3045:2) leaked 14 bytes
/opt/Odin-dev-2025-01/core/strings/strings.odin(3045:2) leaked 14 bytes
/opt/Odin-dev-2025-01/core/strings/strings.odin(3045:2) leaked 14 bytes
/opt/Odin-dev-2025-01/core/strings/strings.odin(3045:2) leaked 15 bytes
/opt/Odin-dev-2025-01/core/strings/strings.odin(3045:2) leaked 16 bytes
No bad `free`

To add some context to the issue, both projects are text-based games and relatively small:

github.com/AlDanial/cloc v 1.96  T=0.02 s (1165.9 files/s, 200300.2 lines/s)
Language                     files          blank        comment           code
Odin                            24           1036            575           2512

github.com/AlDanial/cloc v 1.96  T=0.07 s (382.3 files/s, 157266.9 lines/s)
Language                     files          blank        comment           code
Odin                            25           2066           1422           6796

Using the reported lines of the core lib, I’ve been trying to find out which operations in my code cause the leaks. I have added check points and debugging code and made many tests, but so far I have no clue.

Any advice will be appreciated.

you could patch (and probably pr it) the core lib to propagate the caller locations each time it reports a location in the core lib.

usually that means adding loc := #caller_location to the parameter list of the calling function and then adding loc=loc to the call. Repeat until all the calls end up in your own code.

3 Likes

Try running with sanitizers:

odin build . -debug -sanitize:address
3 Likes

You can also have a look at the tracking allocator in this package: GitHub - laytan/back: Backtraces for Odin. which prints a full stack trace.

3 Likes

Good idea. I had read about it in Zylinski’s book, but somehow I wrongly understood it was intended for the application code.

It seems the solution to make the bundled tracking allocator work in these cases.

I didn’t know the back backtracer. It works great, it traces the allocations up to their origin in my code.