Struct field destructor not called when exception is thrown in the main struct destructor
tchaloupka
chalucha at gmail.com
Fri Oct 16 13:12:04 UTC 2020
Found a pretty nasty bug in vibe-d:
https://github.com/vibe-d/vibe.d/issues/2484
And it's caused by this behavior.
```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 (and it's
bad on it's own but can easily happen as destructors aren't
nothrow @nogc by default).
Is this behavior expected?
More information about the Digitalmars-d-learn
mailing list