HashMap: panic in element destructor causes leaks of unrelated elements #132222
Labels
A-collections
Area: `std::collection`
A-destructors
Area: Destructors (`Drop`, …)
C-discussion
Category: Discussion or questions that doesn't represent real issues.
I-memleak
Issue: Runtime memory leak without `mem::forget`.
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
When dropping a
HashMap
(orHashSet
) and an element's destructor panics, then all elements that would be dropped after are leaked. This is inconsistent with other std collections (Vec
,LinkedList
,BTreeMap
), where after panic in one destructor the remaining destructors are still called, potentially causing an abort if another one panics.I tried this code: playground
I expected to see this happen: The drop behavior of
Vec
,LinkedList
,BTreeMap
andHashMap
should be consistent.Instead, this happened:
HashMap
drops only one element if the destructor unwinds, but the others drop both elements.running Miri on the code shows a memory leak
Meta
rustc --version --verbose
:@rustbot label T-libs A-collections A-destructors I-memleak
The text was updated successfully, but these errors were encountered: