Bug 19141 – Destructors are called for multiple anonymous union members
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2018-08-05T17:44:11Z
Last change time
2018-08-05T18:49:06Z
Assigned to
No Owner
Creator
Paul Backus
Comments
Comment #0 by snarwin+bugzilla — 2018-08-05T17:44:11Z
Example code:
---
import std.stdio: writeln;
struct S1
{
~this() { writeln("Destroying S1"); }
}
struct S2
{
~this() { writeln("Destroying S2"); }
}
struct S3
{
union
{
S1 one;
S2 two;
}
~this() { writeln("Destroying S3"); }
}
void main()
{
S3 three;
}
---
Output:
Destroying S3
Destroying S2
Destroying S1
Since `one` and `two` are members of the same union, calling both destructors will always lead to undefined behavior.
Changing the anonymous union to a named union causes both the S1 and S2 destructor calls to be omitted.
Possibly related to Issue 4421.
Comment #1 by simen.kjaras — 2018-08-05T18:49:06Z
*** This issue has been marked as a duplicate of issue 19122 ***