Bug 21442 – Calling AA.remove from a destructor might lead to InvalidMemoryOperationError

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-12-01T09:58:02Z
Last change time
2020-12-21T01:01:39Z
Keywords
pull
Assigned to
No Owner
Creator
Mathias LANG

Comments

Comment #0 by pro.mathias.lang — 2020-12-01T09:58:02Z
Calling `AA.remove` might shrink the AA, and shrinking an AA allocates. Hence, calling `AA.remove` from a GC-run destructor is not possible as it could lead to an `InvalidMemoryError` being thrown. Example: ``` import core.memory; size_t[size_t] glob; class Foo { size_t count; this (size_t entries) @safe { this.count = entries; foreach (idx; 0 .. entries) glob[idx] = idx; } ~this () @safe { foreach (idx; 0 .. this.count) glob.remove(idx); } } void main () { bar(); GC.collect(); // Needs to happen from a GC collection } void bar () @safe { Foo f = new Foo(16); } ``` This triggers: ``` core.exception.InvalidMemoryOperationError@src/core/exception.d(647): Invalid memory operation ---------------- ``` It's absolutely ridiculous that one can't call `AA.remove` in a destructor (provided the AA is known to be reachable by other means). For reference, vibe.d (eventcore actually) does this.
Comment #1 by dlang-bot — 2020-12-02T18:56:19Z
@Geod24 created dlang/druntime pull request #3301 "Fix 21442: Do not call shrink for AA in finalizers" fixing this issue: - Fix 21442: Do not call shrink for AA in finalizers This can easily lead to InvalidMemoryOperation (e.g. in Vibe.d). https://github.com/dlang/druntime/pull/3301
Comment #2 by dlang-bot — 2020-12-02T23:32:46Z
dlang/druntime pull request #3301 "Fix 21442: Do not call shrink for AA in finalizers" was merged into stable: - c5d3156ee98650f8055c39247f4e626b1b3abe11 by Geod24: Fix 21442: Do not call shrink for AA in finalizers This can easily lead to InvalidMemoryOperation (e.g. in Vibe.d). https://github.com/dlang/druntime/pull/3301
Comment #3 by dlang-bot — 2020-12-21T01:01:39Z
dlang/druntime pull request #3315 "merge stable" was merged into master: - 5a6b41f69a8749a0c30d3a8bbe790aff6d91e175 by Geod24: Fix 21442: Do not call shrink for AA in finalizers This can easily lead to InvalidMemoryOperation (e.g. in Vibe.d). https://github.com/dlang/druntime/pull/3315