Struct field destructor not called when exception is thrown in the main struct destructor

Ali Çehreli acehreli at yahoo.com
Fri Oct 16 15:11:09 UTC 2020


On 10/16/20 6:12 AM, tchaloupka wrote:

 > struct Foo {
 >      Bar bar;
 >      bool err;
 >
 >      ~this() {
 >          // scope(failure) destroy(bar); // < this fixes the Bar
 > destructor call
 >          enforce(!err, "Test err");

Well, that check means "cannot continue", which means the compiler stops 
executing the destruction code because it can't. (It would a serious bug 
if it continued execution in a state that the program knows to be invalid.)

 >      }

Conceptually, bar's destructor is called on that closing brace but we 
decided to abort mission earlier. Your calling destroy(bar) may or may 
not be wrong in case of 'err'. Only you know at that point.

 > Is this behavior expected?

Yes.

Ali



More information about the Digitalmars-d-learn mailing list