在Release设置中进行编译会引入无限循环

时间:2014-07-29 09:00:00

标签: c# .net compiler-optimization

此循环是简单寻路算法的一部分。问题是当在Release模式下构建时,程序永远不会离开这个循环。 我们继续前进桌面'这是一个二维数组,StepGrid数组保持从起始点(pZero)到达每个对应点(表)所需的步数。 当使用Release和Debug设置运行附加到Visual Studio 2012调试器时,此代码可以正常工作。使用Debug设置编译时工作正常。 使用Release设置进行编译时,此代码不起作用。

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

1 个答案:

答案 0 :(得分:0)

感谢那些家伙评论我的问题,我可以找到解决方案。 由于处理器架构以及寄存器中这些数字的处理方式,Debug和Release之间的浮点数比较存在一点不一致。 解决方案是为比较增加一个容差,所以这一行

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

应该改为

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