Thread overview
[Issue 20285] Struct destructor called multiple times in dynamic arrays
Oct 08, 2019
kinke
Oct 09, 2019
Max Samukha
Dec 17, 2022
Iain Buclaw
October 08, 2019
https://issues.dlang.org/show_bug.cgi?id=20285

kinke <kinke@gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kinke@gmx.net

--- Comment #1 from kinke <kinke@gmx.net> ---
This shows better what's going on:

void main() {
    Foo[] array;
    array.length = 1;
    array[0] = Foo(1);
    printf("1st pointer: %p\n", array.ptr);
    array.length = 2;
    array[1] = Foo(2);
    printf("2nd pointer: %p\n", array.ptr);
}

=>

Destroying foo 0
1st pointer: 0x7f9cf69b3000
Destroying foo 0
2nd pointer: 0x7f9cf69b4000
Destroying foo 1
Destroying foo 2
Destroying foo 1

Increasing the length default-constructs instances at the end, which are destructed when assigning the literals later. These are the first 2 dtor calls.

As a reallocation takes places, the two GC arrays are destructed on program termination, these are the other 3 dtor calls. Here, the 1st 1-element-array should probably not be destructed, as it's been moved from (and it hasn't been reset to T.init either).

--
October 09, 2019
https://issues.dlang.org/show_bug.cgi?id=20285

Max Samukha <maxsamukha@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxsamukha@gmail.com

--- Comment #2 from Max Samukha <maxsamukha@gmail.com> ---
Bugs like this should not be marked normal. They are critical at least.

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=20285

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--