Bug 20379 – Cannot destroy associative arrays AAs (Destructor not called on values)

Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2019-11-10T11:34:57Z
Last change time
2021-09-20T14:02:41Z
Assigned to
No Owner
Creator
Johannes Pfau

Comments

Comment #0 by johannespfau — 2019-11-10T11:34:57Z
Tested with 2.089: ------------------------------------------------- import std.stdio; struct Test { ~this(){writefln("~this %x", &this);} } void main() { Test[int] cache; cache[0] = Test(); writeln("destroy"); destroy(cache); writeln(cache); writeln("destroyed"); } ------------------------------------------------- destroy [] destroyed ~this 7f521c9d8004 This breaks deterministic destruction (e.g. of reference counted objects). Replacing destroy with this: ------------------------------------------------- foreach(key, ref val; cache) destroy(val); cache = null; ------------------------------------------------- destroy ~this 7f9b3f65d004 [] destroyed ~this 7f9b3f65d004 So this can be used as a workaround for types which do not mind being destructed to often, such as Reference Counted Types. For all other types, this will double-destruct it.
Comment #1 by schveiguy — 2021-09-20T14:02:41Z
I think this needs to be marked invalid. An AA can provide reference to its values, which is why removing the key doesn't destroy the value. The same thing happens for dynamic arrays. Destroy the array, and it basically just sets it to null. Same thing with a pointer, destroying it just sets it to null. While maybe not desirable, it is consistent behavior.