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.
Comment #2 by bugzilla — 2014-10-31T22:50:13Z
Comment #3 by github-bugzilla — 2014-11-16T15:16:42Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/bdfd00f4130e44c10f41442b05af465b1b0fa8d8 fix Issue 13095 - Someteims destructor is called if constructor throws https://github.com/D-Programming-Language/dmd/commit/31326431312a01dda10759cf47c0d5d617f7009c Merge pull request #4101 from WalterBright/fix13095 fix Issue 13095 - Sometimes struct destructor is called if constructor throws
Comment #4 by github-bugzilla — 2015-02-18T03:39:00Z