marcboeker / go-duckdb

go-duckdb provides a database/sql driver for the DuckDB database engine.
MIT License
646 stars 97 forks source link

[Fix] Memory leak with C.duckdb_struct_type_child_name #138

Closed taniabogatsch closed 8 months ago

taniabogatsch commented 8 months ago

This PR ensures that go-duckdb frees C.duckdb_struct_type_child_name(ty, j) after retrieving the C.GoString. Otherwise, this causes a memory leak of 16 bytes per function call. I also removed the defer to avoid warnings such as Possible resource leak, defer is called in the for loop.

Screenshot 2023-12-21 at 11 24 16
marcboeker commented 8 months ago

@taniabogatsch Awesome catch, thanks for the PR!

michaelmdresser commented 8 months ago

@taniabogatsch this is neat! How did you discover this? What tool are you using?

taniabogatsch commented 8 months ago

@michaelmdresser, I discovered this in a scenario that runs the same query repeatedly on a table with a struct column. The memory kept growing while ignoring duckdb's memory limit until the process crashed. As for the tool, I am using Instruments, which comes as part of Xcode (which I don't use 😄). I can highly recommend it! I'm using both its Leaks analyzer, and its Time Profiler very frequently.