On 10/22/2014 08:45 AM, eles wrote:> C++ versions:
>
>      { //displays ~C~B~A
>          A foo;
>          B bar;
>          C *caz = new C();
>          delete caz;
>      }
>
>      std::cout << std::endl;
>
>      { //displays ~C~B~A
>          std::unique_ptr<A> foo = std::make_unique<A>();
>          std::unique_ptr<B> bar = std::make_unique<B>();
>          C *caz = new C();
>          delete caz;
>      }
>
>
> D version:
>
>      { //displays ~A~B~C
>          A foo = scoped!(A)();
>          B bar = scoped!(B)();
>          C caz = new C();
>          destroy(caz);
>      }
>
> Why the objects are not destroyed in the inverse order of their
> creation? Case in point, destroying foo releases a lock for bar and caz.

I confirm this. If you put writeln() expressions inside typecons.scoped, you will realize that the destroys are happening right after each scoped line:

constructed A
destroying
~A
constructed B
destroying
~B
~C

Extremely dangerous and very tricky! The solution? Define the objects as 'auto' (or 'const', etc.), which you want anyway:

    auto foo = scoped!(A)();
    auto bar = scoped!(B)();

You do not want to be left with A or B. You want Scoped!A and Scoped!B.

Ali

Reply via email to