<p dir="ltr"><br>
On 20 Nov 2014 01:45, "Koz Ross via D.gnu" <<a href="mailto:d.gnu@puremagic.com">d.gnu@puremagic.com</a>> wrote:<br>
><br>
> The bug is similar to <a href="https://issues.dlang.org/show_bug.cgi?id=6998">https://issues.dlang.org/show_bug.cgi?id=6998</a>. Specifically, here is the code that can be used to demonstrate it. I'm using GDC 4.9.1 (according to gdc --version).<br>
><br>
> module badbh;<br>
><br>
> import std.container, std.string, std.stdio;<br>
><br>
> class Node {<br>
>   size_t val;<br>
>   Node next;<br>
><br>
>   this (size_t val) {<br>
>     this.val = val;<br>
>   }<br>
><br>
>   override string toString() {<br>
>     if (next is null) {<br>
>       return format("%s", val);<br>
>     } else {<br>
>       return format("%s, %s", val, next.toString());<br>
>     }<br>
>   }<br>
> }<br>
><br>
> void main () {<br>
>   //sets up the demo structure<br>
>   auto n = new Node(40);<br>
>   n.next = new Node(30);<br>
>   n.next.next = new Node(10);<br>
>   n.next.next.next = new Node(20);<br>
>   writeln(n); //should yield "40, 30, 10, 20"<br>
>   auto t2 = top2(n);<br>
>   writeln(n); //watch the segfaults<br>
> }<br>
><br>
> Node[] top2 (Node n) {<br>
>   auto heap = BinaryHeap!(Array!Node, function bool (Node a, Node b){return a.val > b.val;})();<br>
>   Node[] nodes;<br>
>   auto ptr = n;<br>
>   while (ptr !is null) {<br>
>     heap.insert(ptr);<br>
>     ptr = ptr.next;<br>
>   }<br>
>   for (auto i = 0; i < 2; i++) {<br>
>     nodes ~= heap.front;<br>
>     heap.removeFront;<br>
>   }<br>
>   assert(nodes.length == 2);<br>
>   //some sanity checks to show my code isn't logically FUBAR<br>
>   assert(nodes[0] !is null);<br>
>   assert(nodes[1] !is null);<br>
>   assert(nodes[0].val == 10);<br>
>   assert(nodes[1].val == 20);<br>
>   return nodes;<br>
> }<br>
><br>
> The comments are self-explanatory. This destructive behaviour occurs *only* when the heap is in a different scope to the structure - if you were to copy all the code in top2 into main, the segfault wouldn't happen.<br>
><br>
> I know that this is definitely a GDC thing - the same code under DMD works correctly (i.e. prints the whole linked structure and doesn't segfault).<br></p>
<p dir="ltr">Looking at the date it was fixed in DMD, the next release merge should resolve that.</p>
<p dir="ltr">Iain.</p>