释放模式导致在调试模式下工作的循环中的无限循环

问题描述

因此,下面是一个while循环,该循环可在调试中工作,但在释放模式下无限循环。

Ans FindMaxProj(polyShape& S,Matrix<float,3,1>& N,1>& cg,int StartInd){
    MaxFound=false;
    MaxIndInit=StartInd;
    MaxValue=(cg+S.Verts.col(MaxIndInit)).transpose()*N;

    while(!MaxFound){  //Infinitely loops in release mode,works in debug
        MaxFound=true;
        MaxInd=MaxIndInit;
        for(int i=0; i<S.EdgeLinks[MaxInd].size();i++){
            proj=(cg+S.Verts.col(S.EdgeLinks[MaxInd][i])).transpose()*N;
            if(proj>MaxValue){
                MaxFound=false; //Causes infinite looping in release mode
                MaxValue=proj;
                MaxIndInit=S.EdgeLinks[MaxInd][i];
            }
        }
        MaxInd=MaxIndInit;
    }

    MaxCall.Value=MaxValue;
    MaxCall.Ind=MaxInd;

    return MaxCall;
}

我知道它会无限循环,因为如果我为发布模式添加一个迭代上限,循环将成功结束。 S.EdgeLinks是int向量的向量。我正在将Eigen用于矩阵数学。同样,传入的S是一个extern变量。我也使用CodeBlocks和认的编译器设置。请让我知道是否需要提供更多信息。

解决方法

这是浮点数的舍入错误。我将float变量“ proj”四舍五入到小数点后五位,以便if检查不会被错误触发,并且现在可以使用了。