Should __ArrayDtor be called for uninitialised arrays?

Teodor Dutu teodor.dutu at gmail.com
Thu Nov 18 12:27:54 UTC 2021


Hi,

The following code calls `__ArrayDtor` at the end of `main`. 
However, since `arr` is uninitialised, `free(p)` produces a 
segmentation fault.
```d
struct S
{
     int *p;

     ~this()
     {
         free(p);
     }
}

void main()
{
     S[3] arr = void;
}
```

Is this the expected behaviour? One possible fix is to let the 
programmer handle the destruction of void-initialised arrays 
themselves.

I am asking because, in 
[`_d_arrayctor`](https://github.com/dlang/druntime/blob/0254b3f3dd263fbf7496354c6fcb53026fa44098/src/core/internal/array/construction.d#L73), the `throw` introduces call to `__ArrayDtor`, which destroys the array `to`. This call is unnecessary, as the `_d_arrayctor` already destroys the initialised elements of `to`, while the uninitialised ones need not be destroyed, as I said above.

Thanks,
Teodor


More information about the Digitalmars-d mailing list