[Issue 21322] New: Struct field destructor not called when exception is thrown in the main struct destructor
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Oct 17 11:59:34 UTC 2020
https://issues.dlang.org/show_bug.cgi?id=21322
Issue ID: 21322
Summary: Struct field destructor not called when exception is
thrown in the main struct destructor
Product: D
Version: D2
Hardware: x86_64
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: chalucha at gmail.com
How to reproduce:
```D
import std;
struct Foo {
Bar bar;
bool err;
~this() {
// scope(failure) destroy(bar); // < this fixes the Bar destructor call
enforce(!err, "Test err");
}
}
struct Bar {
static int refs;
~this() { refs--; }
}
void main()
{
{
Foo f;
Bar.refs = 1;
}
assert(Bar.refs == 0);
try () {
Foo f;
f.err = true;
Bar.refs = 1;
}();
catch (Exception ex) {}
assert(Bar.refs == 0);
}
```
So when the exception is thrown within Foo destructor, Bar's destructor isn't
called anymore.
--
More information about the Digitalmars-d-bugs
mailing list