www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21989] New: [REF 2.096] Double destruction of classes since

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

          Issue ID: 21989
           Summary: [REF 2.096] Double destruction of classes since
                    `-preview=dtorfields` became the default
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: industry, wrong-code
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: pro.mathias.lang gmail.com

```D
import std;

pragma(msg, __VERSION__);

class Test {
    S s;

    this()
    {
        if (uniform(1, 10) != 0)
                throw new Exception("oops");
        s = S(13);
    }
}

struct S {
    int x = 42;
    int y;
    this(int y) { this.y = y; };
    this(this) { assert(x == 42); }
    ~this()
    {
        if (x != 42) writeln("OH NO!");
        x = 0; // omitting this makes "OH NO!" go away
    }
}

void main()
{
    try new Test;
    catch (Exception e) writeln("Expected fail: ", e.msg);
}
```

Result:
```
2096L
Expected fail: oops
OH NO!
```

This happens because the class ctor destructs it, then the GC, upon collecting
the object, finalize the class again.

--
Jun 02