Skip to main content

DestroyFn

Type Alias DestroyFn 

Source
pub type DestroyFn = unsafe extern "C" fn(states: *mut duckdb_aggregate_state, count: idx_t);
Expand description

Frees memory allocated by StateInitFn.

Called after finalize, on the source states of a CombineFn once they have been merged, and on states that are never finalized. Must free all heap allocations made in StateInitFn.

DuckDB does not call it for every state it created: not for a grouped aggregate’s states a stopped result scan never reached (docs/upstream-duckdb-reports.md, item 20; 1.4.4 to 2.0.0), and, before 2.0.0, not for the states a window frame with EXCLUDE initialises for each row (item 35; fixed in 2.0.0). What those states own on the heap leaks.

§Not every state it receives was initialised

When a StateInitFn call fails, DuckDB 1.4.4 to 1.5.5 (not re-measured on 1.5.6) still destroys every state row it had created, including rows whose state_init never ran — for this aggregate and for any other in the same query (RowOperations::InitializeStates is not exception-safe). In one run against 1.5.5, a failure at init call 3001 sent 4096 states to the destructor, 1096 of them never initialised. A destructor that frees a pointer stored by state_init must therefore recognise a slot state_init did not write; FfiState does so with a tag. The same failure also leaves most initialised states undestroyed, so what they own leaks; see docs/upstream-duckdb-reports.md. A failed finalize leaves states undestroyed too: in a grouped query on every release up to 2.0.0, and in an ungrouped one before 2.0.0 (see AggregateFunctionInfo::set_error).