Bug 13095 – Sometimes struct destructor is called if constructor throws
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-07-11T09:17:00Z
Last change time
2015-02-18T03:39:00Z
Keywords
wrong-code
Assigned to
nobody
Creator
verylonglogin.reg
Comments
Comment #0 by verylonglogin.reg — 2014-07-11T09:17:03Z
This code should run fine:
---
import std.stdio;
bool b = false;
struct S
{
this(int) { throw new Exception(""); }
~this() { b = true; }
}
void main()
{
try S(0); catch(Exception) { }
assert(!b); // fails
}
---
Also if a `Throwable` is thrown in destructor it is called recursively:
---
import std.stdio;
struct S
{
this(int)
{
throw new Exception("");
}
~this()
{
int p;
asm { mov p, ESP; }
writefln("~this, ESP: 0x%X", p);
throw new Error(""); // calls `~this`
}
}
void main()
{
try
S(0);
catch(Exception)
writeln("catch Exception");
writeln("end main");
}
---
<prints '~this, ESP: 0x...' until stack overflows>
---
This is a major issue breaking RAII code.
Comment #1 by verylonglogin.reg — 2014-07-11T09:24:15Z
Note:
Replacing `S(0)` with `S s = S(0)` or `f(S(0))` detriggers the issue.