July 08, 2004
I see there are some open issues about auto (such as order of destruction), but I'm not sure if this is one of them.  The documentation says:

[quote]

When an auto class reference goes out of scope, the destructor (if any) for it is automatically called. This holds true even if the scope was exited via a thrown exception.

[/quote]

Which is fine except that it seems to be true even if the constructor of the auto object itself throws an exception.  Here's some code typical of the RAII idiom which demonstrates the problem:

[code]

import std.c.stdio;
import std.c.stdlib;
import std.string;

auto class A
{
    this(int i)
    {
        if (i > 0)
        {
            throw new Exception("A ctor exception.");
        }
    }
    ~this()
    {
        printf("A dtor.\n");
    }
}

auto class B
{
    this(int i)
    {
        if (i <= 0)
        {
            throw new Exception("B ctor exception.");
        }
    }
    ~this()
    {
        printf("B dtor.\n");
    }
}

int
main(char[][] argv)
{
    int i = -1;

    if (argv.length > 1)
    {
        i = atoi(argv[1]);
    }

    try
    {
        printf("Creating A.\n");

        auto A a = new A(i);

        printf("A created.\n");

        printf("Creating B.\n");

        auto B b = new B(i);

        printf("B created.\n");
    }
    catch (Exception ex)
    {
        printf("%.*s\n", ex.toString());
    }
    finally
    {
        printf("finally.\n");
    }

    return (EXIT_SUCCESS);
}

[/code]

Run without an argument (using dmd 0.94) it prints:

Creating A.
A created.
Creating B.
A dtor.
B ctor exception.
finally.
B dtor.

I was happy until the last line.  Naively, I would think the destructor for B should not be run since the ctor did not finish.

Run with an argument of 9 it prints:

Creating A.
A ctor exception.
finally.
A dtor.

Bug or no bug?

BTW, is there an official bug list?  (I found the pending peeves list on the wiki, but this doesn't seem to be an offical bug list.)

Dave

-- 
D.a.v.i.d T.i.k.t.i.n
t.i.k.t.i.n [at] a.d.v.a.n.c.e.d.r.e.l.a.y [dot] c.o.m