www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Access violation using chain()

reply "Brad Anderson" <eco gnuk.net> writes:
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
Apr 19 2012
next sibling parent travert phare.normalesup.org (Christophe) writes:
"Brad Anderson" , dans le message (digitalmars.D.learn:34902), a écrit :
 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
      }

My guess is that chain takes lst by reference, just like the delegates for map, wo both are working on the same slice instance. The chain first pops elements from lst, and then calls the mapped sequence. At that time, lst is empty. You can just copy lst before you give it to chain (or to map's delegate) to solve this bug: auto range_gen(string[] lst) { auto a = sequence!"n+1"().map!(a=>format("%s%d", lst[0], a))(); string[] lst2 = lst; return chain(lst2, a); // access violation }
Apr 19 2012
prev sibling next sibling parent Brad Anderson <eco gnuk.net> writes:
--bcaec554d78aed776804be0a86d3
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Thu, Apr 19, 2012 at 7:25 AM, Christophe <travert phare.normalesup.org>w=
rote:

 "Brad Anderson" , dans le message (digitalmars.D.learn:34902), a =E9crit =

 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 =3D ["a", "b"];
          auto rng =3D range_gen(lst);
          writeln(rng.take(5));
      }
      auto range_gen(string[] lst)
      {
          auto a =3D sequence!"n+1"().map!(a=3D>format("%s%d", lst[0],
 a))();
          return chain(lst, a); // access violation
          //return a; // works
      }

My guess is that chain takes lst by reference, just like the delegates for map, wo both are working on the same slice instance. The chain first pops elements from lst, and then calls the mapped sequence. At that time, lst is empty. You can just copy lst before you give it to chain (or to map's delegate) to solve this bug: auto range_gen(string[] lst) { auto a =3D sequence!"n+1"().map!(a=3D>format("%s%d", lst[0], a))(); string[] lst2 =3D lst; return chain(lst2, a); // access violation }

consumption tricks me more often than I wish. I'll eventually learn to look out for it more actively. Regards, Brad Anderson --bcaec554d78aed776804be0a86d3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Thu, Apr 19, 2012 at 7:25 AM, Christophe <span dir=3D"ltr">&lt;<a href= =3D"mailto:travert phare.normalesup.org">travert phare.normalesup.org</a>&g= t;</span> wrote:<br><div class=3D"gmail_quote"><blockquote class=3D"gmail_q= uote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1e= x"> &quot;Brad Anderson&quot; , dans le message (digitalmars.D.learn:34902), a = =E9crit=A0:<br> <div class=3D"im">&gt; Perhaps I&#39;m just misunderstanding something abou= t closures but<br> &gt; the following code seems to behave oddly:<br> &gt;<br> &gt; =A0 =A0 =A0import std.stdio, std.range, std.algorithm, std.string;<br> &gt;<br> &gt; =A0 =A0 =A0void main()<br> &gt; =A0 =A0 =A0{<br> &gt; =A0 =A0 =A0 =A0 =A0auto lst =3D [&quot;a&quot;, &quot;b&quot;];<br> &gt; =A0 =A0 =A0 =A0 =A0auto rng =3D range_gen(lst);<br> &gt; =A0 =A0 =A0 =A0 =A0writeln(rng.take(5));<br> &gt; =A0 =A0 =A0}<br> &gt; =A0 =A0 =A0auto range_gen(string[] lst)<br> &gt; =A0 =A0 =A0{<br> &gt; =A0 =A0 =A0 =A0 =A0auto a =3D sequence!&quot;n+1&quot;().map!(a=3D&gt;= format(&quot;%s%d&quot;, lst[0],<br> &gt; a))();<br> &gt; =A0 =A0 =A0 =A0 =A0return chain(lst, a); // access violation<br> &gt; =A0 =A0 =A0 =A0 =A0//return a; // works<br> &gt; =A0 =A0 =A0}<br> <br> </div>My guess is that chain takes lst by reference, just like the delegate= s<br> for map, wo both are working on the same slice instance. The chain first<br=

time, lst is empty.<br> <br> You can just copy lst before you give it to chain (or to map&#39;s delegate= )<br> to solve this bug:<br> <div class=3D"im"><br> auto range_gen(string[] lst)<br> {<br> =A0 =A0 auto a =3D sequence!&quot;n+1&quot;().map!(a=3D&gt;format(&quot;%s= %d&quot;, lst[0], a))();<br> </div> =A0 =A0 string[] lst2 =3D lst;<br> =A0 =A0 return chain(lst2, a); // access violation<br> }<br> <br> </blockquote></div><br><div>Ah, that would make sense. =A0I&#39;ll test and= make sure when I get home. Range consumption tricks me more often than I w= ish. =A0I&#39;ll eventually learn to look out for it more actively.</div><d= iv> <br></div><div>Regards,</div><div>Brad Anderson</div> --bcaec554d78aed776804be0a86d3--
Apr 19 2012
prev sibling parent "Brad Anderson" <eco gnuk.net> writes:
On Thursday, 19 April 2012 at 16:19:05 UTC, Brad Anderson wrote:
 On Thu, Apr 19, 2012 at 7:25 AM, Christophe 
 <travert phare.normalesup.org>wrote:

 "Brad Anderson" , dans le message (digitalmars.D.learn:34902), 
 a écrit :
 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
      }

My guess is that chain takes lst by reference, just like the delegates for map, wo both are working on the same slice instance. The chain first pops elements from lst, and then calls the mapped sequence. At that time, lst is empty. You can just copy lst before you give it to chain (or to map's delegate) to solve this bug: auto range_gen(string[] lst) { auto a = sequence!"n+1"().map!(a=>format("%s%d", lst[0], a))(); string[] lst2 = lst; return chain(lst2, a); // access violation }

home. Range consumption tricks me more often than I wish. I'll eventually learn to look out for it more actively. Regards, Brad Anderson

Ok, so this wasn't the problem. I have no idea what the problem is. Using string[] lst = list; doesn't help nor does lst.save. I can get some really weird behavior messing around with this. Access violations that appear or disappear when symbolic debug info is enabled. Here's an example of a weird one: import std.stdio, std.range, std.algorithm, std.string; void main() { writeln(gen(["a", "b"]).take(5)); } auto gen(string[] lst) { auto prefix = lst[0]; auto a = iota(10).map!(a=>format("%s%d", prefix, a))(); //auto a = sequence!"n+1"().map!(a=>format("%s%d", prefix, a))(); return chain(lst.save, a); } Compiling that using DMD 2.059 without -g results in a UTF exception (memory corruption?) after outputting ["a", "b",. Compiling with -g produces no exception but the output is incorrect (["a", "b", "0", "1", "2"] should be ["a", "b", "a0", "a1", "a2"]). Now, if you replace the iota line with with the commented out sequence line it works the other way. Without -g gives no exception (but correct output this time), with -g results in a UTF exception. It's a bit unsettling that adding debug info can change the behavior of the program. I suppose this makes this a bug but I'm not sure what I'd call it. Is it a bug with chain() or with closures? Is the symbolic debug information differences a different bug? Regards, Brad Anderson
Apr 22 2012