[Issue 13095] New: Someteims `struct` destructor is called if constructor throws

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Jul 11 02:17:03 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=13095

          Issue ID: 13095
           Summary: Someteims `struct` destructor is called if constructor
                    throws
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: wrong-code
          Severity: major
          Priority: P1
         Component: DMD
          Assignee: nobody at puremagic.com
          Reporter: verylonglogin.reg at gmail.com

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.

--


More information about the Digitalmars-d-bugs mailing list