September 03, 2015 [Issue 15009] New: Object.destroy doesn't call dtors for object in static arrays | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=15009 Issue ID: 15009 Summary: Object.destroy doesn't call dtors for object in static arrays Product: D Version: D2 Hardware: All OS: All Status: NEW Severity: major Priority: P1 Component: druntime Assignee: nobody@puremagic.com Reporter: nicolas.jinchereau@gmail.com struct S { int x; this(int x) { writeln("ctor"); } this(this) { writeln("ctor(postblit)"); } ~this() { writeln("dtor"); } } void main(string[] args) { S[2]* arr = cast(S[2]*)calloc(1, S.sizeof); emplace(arr, S(1)); destroy(*arr); free(arr); } output has 5 ctors, and 3 dtors: ctor ctor(postblit) ctor(postblit) dtor ctor(postblit) dtor ctor(postblit) dtor fix is to modify this overload of Object.destroy: void destroy(T : U[n], U, size_t n)(ref T obj) if (!is(T == struct)) { typeid(T).destroy(&obj); // +++ obj[] = U.init; } output now has 5 ctors, and 5 dtors, as expected: ctor ctor(postblit) ctor(postblit) dtor dtor dtor ctor(postblit) dtor ctor(postblit) dtor -- |
Copyright © 1999-2021 by the D Language Foundation