Thread overview
[Issue 15009] Object.destroy calls unnecessary postblits for destruction of static arrays object
[Issue 15009] Object.destroy doesn't call dtors for object in static arrays
Sep 03, 2015
Kenji Hara
Sep 03, 2015
Kenji Hara
Sep 03, 2015
Kenji Hara
Sep 03, 2015
bitwise
September 03, 2015
https://issues.dlang.org/show_bug.cgi?id=15009

--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> ---
The bug in Object.destroy overload for the static array is, that is using block assignment.

void destroy(T : U[n], U, size_t n)(ref T obj)
  if (!is(T == struct))
{
    obj[] = U.init;
}

obj[] = U.init; _copies_ the rhs for each elements of lhs, and destroys the original elements of lhs. Instead of that, it should just destroy the elements.

And, the order of destroying should occur from the last to the first.

Then' the fixed destroy function should be:

void destroy(T : U[n], U, size_t n)(ref T obj)
  if (!is(T == struct))
{
    foreach_reverse (ref e; obj[])
        e = U.init;
}

--
September 03, 2015
https://issues.dlang.org/show_bug.cgi?id=15009

Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Object.destroy doesn't call |Object.destroy calls
                   |dtors for object in static  |unnecessary postblits for
                   |arrays                      |destruction of static
                   |                            |arrays object

--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> ---
Note that, destroy actually calls destructors.

import std.stdio, std.conv, core.stdc.stdlib;
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);
    printf("-- calloc done\n");
    emplace(arr, S(1));
    printf("-- emplace done\n");
    destroy(*arr);
    printf("-- destroy done\n");
    //typeid(*arr).destroy(&arr);
    free(arr);
    printf("-- free done\n");
}

output is:

$ dmd -run test
-- calloc done
ctor
ctor(postblit)
ctor(postblit)
dtor
-- emplace done
ctor(postblit)
dtor
ctor(postblit)
dtor
-- destroy done
-- free done

My point is , the two postblit calls invoked by destroy() is unnecessary.

--
September 03, 2015
https://issues.dlang.org/show_bug.cgi?id=15009

Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> ---
https://github.com/D-Programming-Language/druntime/pull/1376

--
September 03, 2015
https://issues.dlang.org/show_bug.cgi?id=15009

--- Comment #4 from bitwise <nicolas.jinchereau@gmail.com> ---
works as expected.
Thanks =)

--
September 05, 2015
https://issues.dlang.org/show_bug.cgi?id=15009

--- Comment #5 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/5be4156ccba77b49c7232060bbecb8b2527a8287
fix Issue 15009 - Object.destroy calls unnecessary postblits for destruction of
static arrays object

https://github.com/D-Programming-Language/druntime/commit/3dc4e28ba997954deeb1f8db63051ba1f03e0463 Merge pull request #1376 from 9rnsr/fix15009

Issue 15009 - Object.destroy calls unnecessary postblits for destruction of static arrays object

--
September 05, 2015
https://issues.dlang.org/show_bug.cgi?id=15009

github-bugzilla@puremagic.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--
October 04, 2015
https://issues.dlang.org/show_bug.cgi?id=15009

--- Comment #6 from github-bugzilla@puremagic.com ---
Commits pushed to stable at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/5be4156ccba77b49c7232060bbecb8b2527a8287
fix Issue 15009 - Object.destroy calls unnecessary postblits for destruction of
static arrays object

https://github.com/D-Programming-Language/druntime/commit/3dc4e28ba997954deeb1f8db63051ba1f03e0463 Merge pull request #1376 from 9rnsr/fix15009

--