[Issue 20379] New: Cannot destroy associative arrays AAs (Destructor not called on values)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Nov 10 11:34:57 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=20379

          Issue ID: 20379
           Summary: Cannot destroy associative arrays AAs (Destructor not
                    called on values)
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: druntime
          Assignee: nobody at puremagic.com
          Reporter: johannespfau at gmail.com

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.

--


More information about the Digitalmars-d-bugs mailing list