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