2 ms·
Part of it is that lifetimes become complicated. Do you convert each variable into a separate object, or do you shove all of them into a single object represent
by int_19h 4y ago
Part of it is that lifetimes become complicated. Do you convert each variable into a separate object, or do you shove all of them into a single object representing all captured variables from that frame?
If the former, it's very space-inefficient if you're capturing lots of stuff, as is typical with callback-oriented programming.
If the latter, then a lambda referring to a single variable from some stack frame will keep all other captured variables in that frame alive. Which can be especially surprising if the lambda that is alive captured a single int, and that causes a huge array that referenced by a different local that can no longer be reached to remain allocated.
FWIW C# does the latter, and it works "good enough" in practice. But I've seen more than one mysterious memory leak caused by it, too - I think most of the time they just aren't big enough for people to notice.