Two bugs found: GC bug as well as scope delegate bug

Simen Kjaeraas simen.kjaras at gmail.com
Mon Aug 8 14:24:09 PDT 2011


On Mon, 08 Aug 2011 03:17:13 +0200, Mehrdad <wfunction at hotmail.com> wrote:

> OK so apparently the GC was working fine, my bad.
> I'm not sure about the compiler, though.
>
> When I have this code:
>
>      void foo(int a, scope void delegate(int) sink)
>      {
>          sink(a);
>      }
>      void main()
>      {
>          int tmp;
>          foo(5, delegate(int c) { tmp = c; });
>      }
>
> I get a heap allocation as soon as main() is entered. It only happens  
> because of the closure.
> Why does this happen? Isn't the whole point of "scope" to prevent this?
> (I didn't post this on the bug tracking system since I'm not sure if  
> it's by design or not.)

'scope' in the position you have it, will not prevent this, no. Instead,
it will have to be used upon declaring the delegate, like this:

void bar(int a, scope void delegate(int) sink)
{
     sink(a);
}
void main()
{
     int tmp;
     scope fn = (int c) { tmp = c; };
     bar(5, fn);
}

-- 
   Simen


More information about the Digitalmars-d mailing list