int inc = swap ? 1 : -1;
for(int j=j1; j!=j2; j+=inc){
if(j < 0)
j = curve2->controlPoints()->size()-1;
if(j >= curve2->controlPoints()->size())
j = 0;
curve->addControlPoint(curve2->controlPoint(j)->pos(), curve2->controlPoint(j)->triangle());
}
I found out that in some case, this for loop infinitely. When looking with a debugger, j does reach j2 but for some reason continue to loop.
I then tried to add a break if j == j2 inside the loop (technically j-inc since j is incremented as it enter into the loop again)
for(int j=j1; j!=j2; j+=inc){
if (j - inc == j2)
{
qDebug() << "break =================================";
break;
}
if(j < 0)
j = curve2->controlPoints()->size()-1;
if(j >= curve2->controlPoints()->size())
j = 0;
curve->addControlPoint(curve2->controlPoint(j)->pos(), curve2->controlPoint(j)->triangle());
}
And doing that indeed solved the problem (and the "break" is indeed printed), but it doesn't really make any sense ?
Why does the first for loop act this way ?
Edit :
I'm iterating over a part of a list (between values j1 and 2). The iteration can go both side depending of the swap parameter (boolean). If j reach one of the end of the list, it continue on the other side (for example if j1=5, j2=1 and the list size is 7, j will take the following values : 5 6 0 1)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…