Access violation using chain()

Brad Anderson eco at gnuk.net
Thu Apr 19 01:01:26 PDT 2012


Perhaps I'm just misunderstanding something about closures but 
the following code seems to behave oddly:

     import std.stdio, std.range, std.algorithm, std.string;

     void main()
     {
         auto lst = ["a", "b"];
         auto rng = range_gen(lst);
         writeln(rng.take(5));
     }
     auto range_gen(string[] lst)
     {
         auto a = sequence!"n+1"().map!(a=>format("%s%d", lst[0], 
a))();
         return chain(lst, a); // access violation
         //return a; // works
     }

Returning with the chain() gives an access violation after the 
writeln has processed the first two elements and gets to the 
elements generated by map(sequence()) (the output is '["a", "b", 
').  If I just return the map(sequence()) it works correctly.  If 
I don't use lst[0] in the map and instead use a literal it works 
without issue.  It also works without issue if I use a global in 
place of lst[0].

I can work around this. Moving the chain() outside to the calling 
function seems to work fine but I was making using of ResultOf to 
type a member variable which is why I had the function in the 
first place (I can manually type it but it's pretty ugly).

Regards,
Brad Anderson


More information about the Digitalmars-d-learn mailing list