www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11329] New: Struct dtor called for a struct with a failed ctor when struct is nested in a class

http://d.puremagic.com/issues/show_bug.cgi?id=11329

           Summary: Struct dtor called for a struct with a failed ctor
                    when struct is nested in a class
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrej.mitrovich gmail.com



07:10:52 PDT ---
-----
module test;

import std.exception;
import std.stdio;

class C
{
    this()
    {
        s = S(1);
    }

    S s;
}

struct S
{
    this(int x)
    {
        _x = x;

        int f;
        if (!f) throw new Exception("");
    }

    ~this()
    {
        stderr.writefln("S dtor -- _x: %s", _x);
    }

    int _x = -1;
}

void main()
{
    // S dtor not called (ok, because its ctor failed)
    assertThrown!Exception(S(1));

    // S dtor called even though S object was not 
    // properly initialized (ctor failed)
    assertThrown!Exception(new C());
}
-----

The S object is left in an improperly-initialized state since its ctor failed
(due to the thrown Exception), but for some reason its dtor /is/ called,
however only in a situation when it's nested in a class.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 23 2013