[Issue 16197] Constructors/postblits and destructors don't match up for array initialisation

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri May 12 04:57:56 PDT 2017


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

Walter Bright <bugzilla at digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla at digitalmars.com
           Severity|critical                    |enhancement

--- Comment #9 from Walter Bright <bugzilla at digitalmars.com> ---
(In reply to Max Samukha from comment #7)
> Should be either two destructors or no postblit. For "Elem arr" instead of
> "Elem[1] arr", only the destructor is correctly called once.

What's happening here is that arrays are constructed by calling
_d_arraysetctor(). That works by passing it an instance of the object Elem
being constructed, which is the Elem.init object, and then copying it into
arr[1]. The copy operation invokes the postblit.

The code doing the postblit is here:

https://github.com/dlang/druntime/blob/master/src/rt/arrayassign.d#L245

Then when it goes out of scope, the destructor is called.

That's why you see a single postblit and a single destructor.

The code is not incorrect, this is not a bug. However, it can be improved to
skip the postblit for default initialization.

Therefore, I'm going to re-categorize this as an enhancement request.

--


More information about the Digitalmars-d-bugs mailing list