[Issue 16388] New: Throwing constructors must destroy fully constructed fields

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Aug 14 06:34:18 PDT 2016


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

          Issue ID: 16388
           Summary: Throwing constructors must destroy fully constructed
                    fields
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: andrei at erdani.com

Per
http://www.digitalmars.com/d/archives/digitalmars/D/What_is_going_on_here_257862.html,
if the constructor of an object constructs a field and then throws an
exception, the field's destructor is not called.

import std.stdio;

struct A {
     int a = 3;

     this( int var ) {
         a += var;
     }

     ~this() {
         writeln("A down ", a);
     }
}

struct B {
     A a;

     this( int var ) {
         a = A(var+1);
         throw new Exception("An exception");
     }
}

void main()
{
     try {
         auto b = B(2);
     } catch( Exception ex ) {
     }
}

The code must print "A down".

--


More information about the Digitalmars-d-bugs mailing list