Bug 19218 – object.destroy should check for classes for static arrays

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-09-04T11:32:18Z
Last change time
2020-02-15T23:04:33Z
Keywords
pull
Assigned to
No Owner
Creator
Jacob Carlborg

Comments

Comment #0 by doob — 2018-09-04T11:32:18Z
The code for "object.destroy" for static arrays looks like: void destroy(T : U[n], U, size_t n)(ref T obj) if (!is(T == struct)) { foreach_reverse (ref e; obj[]) destroy(e); } I think the template constraint is to avoid structs that have an "alias this" pointing to a static array. The same can happen for classes as well.
Comment #1 by shigekikarita — 2019-07-14T18:14:03Z
I agree with you. I made a unittest for checking this --- unittest { struct S { static dtorCount = 0; ~this() { ++dtorCount; } } class C { static dtorCount = 0; ~this() { ++dtorCount; } } struct A(T) { T[3] a; alias a this; } auto as = new A!S; destroy(as); auto ac = new A!C; destroy(as); assert(S.dtorCount == 3); // 0 assert(C.dtorCount == 3); // 0 }
Comment #2 by moonlightsentinel — 2020-02-15T15:08:22Z
(In reply to karita from comment #1) > I agree with you. I made a unittest for checking this [...] That unittest is wrong because it destroys the >pointer< to an A instead of the instance (`destroy` takes its arguments by ref).
Comment #3 by dlang-bot — 2020-02-15T15:29:38Z
@MoonlightSentinel created dlang/druntime pull request #2942 "Fix Issue 19218 - object.destroy should check for classes for static …" fixing this issue: - Fix Issue 19218 - object.destroy should check for classes for static arrays https://github.com/dlang/druntime/pull/2942
Comment #4 by dlang-bot — 2020-02-15T23:04:33Z
dlang/druntime pull request #2942 "Fix Issue 19218 - object.destroy should check for classes for static …" was merged into master: - 453b320bf13239faf73ff969d0095187ac9f0ecc by MoonlightSentinel: Fix Issue 19218 - object.destroy should check for classes for static arrays https://github.com/dlang/druntime/pull/2942