[Issue 8832] Segfault when accessing range returned by function that has delegate referencing local variables

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Oct 26 11:41:41 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8832


Maxim Fomin <maxim at maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim at maxim-fomin.ru


--- Comment #3 from Maxim Fomin <maxim at maxim-fomin.ru> 2012-10-26 11:41:40 PDT ---
(In reply to comment #0)
> import std.algorithm;
> import std.range;
> import std.stdio;
> 
> auto boo() {
>     auto C = [2];
>     return [1,1].map!((a) => C).joiner;
> }
> 
> void main() {
>     writeln(boo().take(12));
> }
> 
> This code will either segfault or produce nonsensical output. Replacing (a)=>C
> with (a)=>[2] makes the problem go away; shortening [1,1] to [1] also makes the
> problem go away, and removing .joiner also makes the problem go away. Removing
> the .map makes the problem go away too.
> 
> The problem is suspected to be the delegate (a)=>C which references the local
> variable C, which goes out of scope when boo() returns. For whatever reason,
> dmd isn't emitting code to allocate the delegate's context on the heap, causing
> a crash when writeln() tries to read the second element off the range. (I'm not
> sure why it doesn't crash with the first element. Maybe luck.)
> 
> Replacing the delegate with function(a)=>[1] makes the problem go away.

The problem is in erroneous treating (a) => C of type void. If this is fixed
to:
- delegate(int a) { return C; }
- (int a) { return C; )
- (int a) => C;
everything works fine.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list