This loop is part of a simple pathfinding algorithm. The problem is that when building in Release mode the program never leaves this loop. We move on a 'Table' that's a two dimension array , the StepGrid array maintains the number of steps that are required to reach each corresponding point ( of Table ) from the starting point ( pZero ). This code works fine when running attached to Visual Studio 2012 debugger with both Release and Debug settings. Works fine when compiled with Debug setting . This code does NOT work when compiled with Release settings.
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;
}