i write a uncorrect opCmp code,
it runs and did't crashed on ubuntu,dmd2.068.2
but crashed on win7 x64,dmd2.068.2
-------------------------------------------------
import std.stdio;
import std.algorithm;
class A
{
override int opCmp(Object o) const
{
return -1;
}
}
void main()
{
A [] arr;
foreach(i;1..100)
{
arr ~= new A;
arr.sort;
writeln(i);
}
}
------------------------------------------------
1
2
3
4
5
6
7
object.Error@(0): Access Violation
----------------
0x004047D2
0x00403773
Comment #1 by schveiguy — 2015-10-01T17:48:13Z
1. You are not calling std.algorithm.sort, you are calling builtin array sort. You need to add the parentheses.
2. I don't know that we need to concern ourselves with invalid opCmp
Comment #2 by bearophile_hugs — 2015-10-01T19:04:10Z
(In reply to Steven Schveighoffer from comment #1)
> 1. You are not calling std.algorithm.sort, you are calling builtin array
> sort. You need to add the parentheses.
>
> 2. I don't know that we need to concern ourselves with invalid opCmp
D warnings should be active on default.
Comment #3 by mzfhhhh — 2015-10-02T09:43:16Z
(In reply to Steven Schveighoffer from comment #1)
> 1. You are not calling std.algorithm.sort, you are calling builtin array
> sort. You need to add the parentheses.
>
> 2. I don't know that we need to concern ourselves with invalid opCmp
yes,i call the builtin array sort...
my actual opCmp code is quite complicated,i check the code a long time
and find there are different results on different platforms.
Comment #4 by schveiguy — 2015-10-02T17:59:03Z
(In reply to mzfhhhh from comment #3)
> yes,i call the builtin array sort...
> my actual opCmp code is quite complicated,i check the code a long time
> and find there are different results on different platforms.
Any chance you can try the std.algorithm.sort? It may have more useful error (or more consistent behavior at least).
Also, can you narrow down the code to a minimal example that causes the undesired behavior?
It's possible there is an issue with the builtin sort. But sort really does require that items it has already sorted are sorted. I'm not sure what steps it assumes are correct.
An access violation doesn't seem like it should happen, but it's possibly due to an optimization that is invalid with an invalid opCmp.