On Tuesday, 29 October 2013 at 17:55:45 UTC, Ali Çehreli wrote:
Continuing the conversation from the following thread:

  http://forum.dlang.org/post/[email protected]

(Note: The OP's example there behaves as expected on git head.)

The programmer thinks that the following delegate returned by foo() will print S(1) because the delegate uses the local 's' which is supposed to live long enough:

import std.stdio;

struct S
{
    int i;
    ~this() { i = 666; }
}

auto foo()
{
    S s = S(1);
    return { writeln(s); } ;  // <-- Uses local s
}

void main()
{
    foo()();
}

However, 's' gets destroyed upon leaving foo() and the delegate is left with a destroyed object; so the program prints S(666).

Is that by design or a bug?

Aside: Never mind that the destroyed object gets destroyed again, and again, and again. :) (Well, presumably after being copied to other object in its dead state, because the address of the object is not the same.) (Put a writeln() inside the destructor to see that in action.)

Ali

According to the Book of Revelations, 666 is the number of the
Beast (Rev. 13:18), and later (Revelations 19:20) it is stated
that the Beast will be thrown into a lake of fire and brimstone.

That may explain the repeated destruction of s :o)

Whether that is a bug or not, I am unsure.

Craig

Reply via email to