D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19218 - object.destroy should check for classes for static arrays
Summary: object.destroy should check for classes for static arrays
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2018-09-04 11:32 UTC by Jacob Carlborg
Modified: 2020-02-15 23:04 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jacob Carlborg 2018-09-04 11:32:18 UTC
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 karita 2019-07-14 18:14:03 UTC
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 moonlightsentinel 2020-02-15 15:08:22 UTC
(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 Dlang Bot 2020-02-15 15:29:38 UTC
@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 Dlang Bot 2020-02-15 23:04:33 UTC
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