[Issue 23556] Array append ignores copy constructor

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Dec 14 19:20:15 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=23556

--- Comment #1 from Steven Schveighoffer <schveiguy at gmail.com> ---
Just to give some clearer picture of how this causes issues:

```d
import std.typecons;
import std.sumtype;
import std.stdio;
import core.memory;

void fun(RefCounted!string foo) {
    SumType!( RefCounted!(string) )[] foo_storage;

    foo_storage ~= SumType!( RefCounted!(string) )(foo);
    foo_storage ~= SumType!( RefCounted!(string) )(foo); // reallocates, but no
copy
    foo_storage = null;
}

void clobber()
{
    int[1000] x = 0x123456;
}

void main(){
    RefCounted!( string ) foo = RefCounted!( string )("blah");

    fun(foo);
    clobber();
    GC.collect(); // collect the items already in the GC
    writeln(foo);
}
```

What happens here is:

1. An array of one element is created with the sumtype.
2. The array cannot hold a second element, so it's reallocated. However, the
copy constructor of SumType is *not* called. Therefore, `foo` has a ref count
of 3, but there are afterwards 4 references (the single element array, the
double-element array, and the original foo on the stack).
4. clobber makes sure the stack doesn't point at the arrays.
5. GC.collect cleans up 3 foo elements, reducing the count to 0 and
deallocating.
6. The writeln is now writing garbage. on my system it just spewed random
memory to the screen.

--


More information about the Digitalmars-d-bugs mailing list