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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Oct 16 17:18:54 PDT 2012


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

           Summary: Segfault when accessing range returned by function
                    that has delegate referencing local variables
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: hsteoh at quickfur.ath.cx


--- Comment #0 from hsteoh at quickfur.ath.cx 2012-10-16 17:18:50 PDT ---
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.

-- 
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