在发布设置中编译引入了无限循环

本文关键字:编译 无限循环 布设置 设置 | 更新日期: 2023-09-27 18:15:37

这个循环是一个简单寻径算法的一部分。问题是,当在Release模式下构建时,程序永远不会离开这个循环。我们在一个二维数组"Table"上移动,StepGrid数组维护从起点(pZero)到达Table的每个对应点所需的步数。此代码在附带Visual Studio 2012调试器(带有发布和调试设置)运行时运行良好。使用调试设置编译时可以正常工作。当使用版本设置编译时,此代码不工作。

  public List<IntPoint> PathFind(IntPoint pZero, IntPoint pEnd)
        {
            float BIGVALUE = 1000000000f;
            IntPoint p0 = pZero;
            List<IntPoint> res = new List<IntPoint>(); 
            //Initialize StepGrid
            StepGrid = new float[TableWidth][];
            for (int x = 0; x < StepGrid.Length; x++)
            {
                StepGrid[x] = new float[TableHeight];
                for (int y = 0; y < StepGrid[x].Length; y++)
                    StepGrid[x][y] = BIGVALUE;
            }
            List<IntPoint> visitandi = new List<IntPoint>() { p0 };
            List<IntPoint> addendi = new List<IntPoint>();
            if (p0.X > StepGrid.Length || p0.Y > StepGrid[0].Length ||
                pEnd.X > StepGrid.Length || pEnd.Y > StepGrid[0].Length)
                return res;
            StepGrid[p0.X][p0.Y] = 0;
            bool progressMade = true; 
            while (progressMade)
            {
                progressMade = false;
                addendi.Clear();
                for (int cp = 0; cp < visitandi.Count; cp++)
                {
                    float pdist = this.StepGrid[visitandi[cp].X][visitandi[cp].Y];
                      // PossibleMoves  is an array containing all the possible relative moves from a given point. MoveLen contains the steps traveled when performing each one of PossibleMoves
                    for (int pm = 0; pm < PossibleMoves.Length; pm++) 
                    {
                        IntPoint p3 = visitandi[cp] + PossibleMoves[pm];
                        if (CanMoveTo(p3)) //if the pixel is white i can move there
                        {
                            float arrivalDist = pdist + MoveLen[pm];
                            float oldDist = StepGrid[p3.X][p3.Y];
                            if ( StepGrid[p3.X][p3.Y]  > arrivalDist)
                            {
                                if (StepGrid[p3.X][p3.Y] >= BIGVALUE)  
                                    addendi.Add(p3);
                                StepGrid[p3.X][p3.Y] = arrivalDist;
                                progressMade = true;
                            }
                        }
                    }
                }
                if (addendi.Count > 0)
                    progressMade = true;
                visitandi.AddRange(addendi);
            }
         .....
       }
        protected bool CanMoveTo(IntPoint p)
        {
            //Table is byte[,] and is a bitmap gray scale image
            if (p.X < TableWidth && p.Y < TableHeight && p.X > -1 && p.Y > -1)
                if (Table[p.X, p.Y] > BrightnessThreshold)//BrightnessThreshold=70
                    return true;
                else
                    return false;
            return false;
        }

在发布设置中编译引入了无限循环

感谢评论我问题的人,我可以找到解决方案。由于处理器架构和寄存器中处理这些数字的方式,在调试和发布之间的浮点数比较中有一点不一致。解决方案是为比较添加一个容差,因此这一行

if ( StepGrid[p3.X][p3.Y]  > arrivalDist)

改为

if ( StepGrid[p3.X][p3.Y]  - arrivalDist > epsilon)