Bug 10972 – aggregate postblit doesn't clean up in case of failure
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-05T07:26:00Z
Last change time
2015-06-17T21:01:38Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
monarchdodra
Comments
Comment #0 by monarchdodra — 2013-09-05T07:26:20Z
Structs will not recursively destroy their members if the construction scheme fails:
//####
import std.stdio;
struct A
{
this(this)
{writeln("copied A");}
~this()
{writeln("destroy A");}
}
struct B
{
this(this)
{
writeln("B says what?");
throw new Exception("BOOM!");
}
~this()
{writeln("destroy B");}
}
struct S
{
A a;
B b;
}
void main()
{
S s1;
S s2;
writeln("----");
try
s2 = s1;
catch
{}
writeln("----");
try
S s3 = s1;
catch
{}
writeln("----");
}
//####
----
copied A
B says what?
----
copied A
B says what?
----
destroy B
destroy A
destroy B
destroy A
//####
Be it CC, or "postblit-based" assignment, first, the "A" element is constructed, the B fails to CC, but the first A is never actually destroyed. Postblit should first destroy all created members before propagating the exception.
This should be pretty standard and expected behavior for struct construction.
The irony is that only static arrays handle this correctly, but only because they have their own (arguably weird) construction scheme.