[Issue 21989] New: [REF 2.096] Double destruction of classes since `-preview=dtorfields` became the default
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Jun 2 11:53:51 UTC 2021
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 at puremagic.com
Reporter: pro.mathias.lang at 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.
--
More information about the Digitalmars-d-bugs
mailing list