Bug 16388 – Throwing constructors must destroy fully constructed fields
Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-08-14T13:34:00Z
Last change time
2016-08-15T06:24:01Z
Keywords
industry, preapproved, safe, spec, wrong-code
Assigned to
nobody
Creator
andrei
Comments
Comment #0 by andrei — 2016-08-14T13:34:18Z
Per http://www.digitalmars.com/d/archives/digitalmars/D/What_is_going_on_here_257862.html, if the constructor of an object constructs a field and then throws an exception, the field's destructor is not called.
import std.stdio;
struct A {
int a = 3;
this( int var ) {
a += var;
}
~this() {
writeln("A down ", a);
}
}
struct B {
A a;
this( int var ) {
a = A(var+1);
throw new Exception("An exception");
}
}
void main()
{
try {
auto b = B(2);
} catch( Exception ex ) {
}
}
The code must print "A down".
Comment #1 by petar.p.kirov — 2016-08-14T18:19:48Z
*** This issue has been marked as a duplicate of issue 14246 ***