Skip to content

HashMap.remove and HashSet.remove release the stored key (#1189) - #1194

Merged
ehartford merged 1 commit into
mainfrom
fix-1189-map-remove-key-leak
Sep 19, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1189-map-remove-key-leak

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Closes #1189. Ownership change, alone in its batch.

with_hashmap_remove copied the value out and cleared the slot's occupancy; the stored key was never dropped. The table is type-erased, so the runtime cannot drop it, and MAP_REMOVE in codegen emitted no drop either. A key that owns memory leaked: one m.remove(k) on a HashMap[str, str] reports debug-alloc: LEAK size=16, leak count=1 — on main and on the installed compiler. MAP_CLEAR and scope exit were already right (they walk live slots with the receiver's sema type).

Fix. with_hashmap_remove_entry transports the stored key's bytes into key_out before the slot is vacated — remove still compares stored keys while it rehashes the entries that follow, so the key cannot be dropped first — and codegen drops it with its real type. A miss leaves the zeroed slot, whose drop frees nothing. The old entry point is kept for Copy keys. Spec §2.3 (D44): transport is not duplication, and the key has exactly one owner, which must release it.

Tests. test/behavior/lib/map_remove_cases.w joins the D44 ownership harness (run --debug-alloc with WITH_DEBUG_ALLOC_SCRIBBLE=1, with and without block reuse, leak count=0, no double free): hits, repeated misses, draining a map to empty and reusing it, Copy and owning keys, HashSet. remove goes back into the traversal cases, where it had been left out so that file could assert zero leaks.

Battery (pinned seed v0.15.2.0): build, :fixpoint, :test-with-audits (drop and move audits inside it), :test-green, :last-green — all rc=0, 1236 s.

Not fixed here: emit-c never drops map elements at all (#1192).

with_hashmap_remove copied the value out and cleared the slot's occupancy;
the stored key was never dropped. The table is type-erased, so the runtime
cannot drop it, and MAP_REMOVE in codegen emitted no drop either: a key that
owns memory leaked 16 bytes per removal (debug allocator: LEAK size=16,
leak count=1 for one m.remove(k) on a HashMap[str, str]; on main and on the
installed compiler). MAP_CLEAR and scope exit were already right: they walk
live slots with the receiver's sema type.

with_hashmap_remove_entry transports the stored key's bytes into key_out
before the slot is vacated - remove still compares stored keys while it
rehashes the entries that follow, so the key cannot be dropped first - and
codegen drops it with its real type. A miss leaves the zeroed slot, whose
drop frees nothing. Spec §2.3 (D44): transport is not duplication, and the
key has exactly one owner, which must release it.

lib/map_remove_cases.w joins the D44 ownership harness (--debug-alloc with
scribble, with and without reuse, zero leaks): hits, repeated misses,
draining a map and reusing it, Copy and owning keys, HashSet. remove goes
back into the traversal cases. emit-c never drops map elements at all: #1192.
Fixes #1189
@ehartford
ehartford merged commit a28462b into main Sep 19, 2026
5 checks passed
@ehartford
ehartford deleted the fix-1189-map-remove-key-leak branch September 24, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HashMap.remove leaks the stored key when the key owns memory

1 participant