Nasty corner case behaviour

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Apr 19 23:18:24 UTC 2019


On Fri, Apr 19, 2019 at 10:17:08PM +0000, Suleyman via Digitalmars-d wrote:
> On Thursday, 18 April 2019 at 05:01:17 UTC, H. S. Teoh wrote:
> > ...
> 
> Sneaky enough.
> The problem is the value copy semantics of S which results in an
> address to a dead stack frame being saved.
> If you make it a ref parameter the issue goes away.
[...]

Using ref solves it for this specific case, but will still exhibit the
same problem if the struct referenced by the ref is a temporary due to
other factors.  E.g., if you pass the struct to a function that then
returns map!(S.method) back to the caller, then the ref will still be
referencing a temporary that goes out of scope while the lambda still
holds the reference.

The underlying problem is that the lambda closes over a variable whose
lifetime is not guaranteed to be at least the lifetime of the lambda.
It's exactly this sort of scoping issues that -dip1000 is supposed to
prevent, but it seems somehow this case was missed.


T

-- 
IBM = I'll Buy Microsoft!


More information about the Digitalmars-d mailing list