How long does the context of a delegate exist?
Steven Schveighoffer
schveiguy at gmail.com
Thu May 27 21:51:55 UTC 2021
On 5/27/21 4:46 PM, IGotD- wrote:
> On Thursday, 27 May 2021 at 18:13:17 UTC, Adam D. Ruppe wrote:
>>
>> If the delegate is created by the GC and stored it will still be
>> managed by the GC, along with its captured vars.
>>
>> As long as the GC can see the delegate in your example you should be
>> OK. But if it is held on to by a C or OS lib, the GC might not see it
>> and you'd have to addRoot or something to ensure it stays in.
>
> With lambdas in C++ you can cherrypick captures the way you want, then
> somehow this can be converted to an std::function and this is where it
> goes on the heap as well.
>
> In D, how does the compiler know what captures it is supposed to store,
> or does it take the whole lot?
For closures, it uses semantic analysis to see which variables are used
in the lambda, and then allocates a block at the start of the function
to hold those variables.
-Steve
More information about the Digitalmars-d-learn
mailing list