[Issue 21745] Closure created in struct constructor passed to class constructor is not heap allocated

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 22 13:34:51 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=21745

--- Comment #3 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to RazvanN from comment #2)
> As a workaround, simply returning `i` in the delegate seems to work
> correctly:
> 
> struct Foo
> {
>     int i;
>     Bar bar;
>     this(int i)
>     {
>         this.i = i;
>         this.bar = new Bar({ return i; });
>     }
> }

I am not entirely sure, but the issue might be that in the original report the
closure variable is `this`, whereas in my workaround the closure variable is
`i`. Therefore, once memory is allocated on the heap, in the first case it
simply saves the `this` pointer (that points to some stack allocated data) not
the actual struct instance.

--


More information about the Digitalmars-d-bugs mailing list