Inexplicable variable behaviour - debug help?

I am having the strangest issue. Building a mesh at runtime from scratch, a task which this particular piece of code successfully does over a hundred times before it crashes on one particular instance. The problem is with parsing an array of strings into a vector. It goes through 22335 vertices with no problem, but on the very last vertex it throws a wrong format exception on the parse line.

objectMesh = new Vector3[int.Parse(numVertices)];
for(j=0;j<objectMesh.length;j++)
{
 var vertices = lines[i+j].Split(";"[0]);
 vertices = cleanStrings(vertices);		
 objectMesh[j] = Vector3(float.Parse(vertices[0]),float.Parse(vertices[1]),float.Parse(vertices[2]));
}
i+=j;

This is how the function works in every other model and for every other vertex of this model. There is a difference in this line in the source text, namely it is the last vertex, and has an extra delimiter(:wink: at the end. This means vertices is length 5 instead of length 4 after the split. I am not accessing verices[3] or [4] though so I don’t see how this could be causing the problem.

I added a few different versions of debug output in to see what was going on. Replacing the last line with:

var temp = new String[3];
for(k = 0; k<3; k++)
{
 if(modelPath.Contains("exec") && j>22000)
 {
   print(k);
   print(vertices[k]);
 }
 var temp2=vertices[k];
 temp[k]=temp2;
}
objectMesh[j] = Vector3(float.Parse(temp[0]),float.Parse(temp[1]),float.Parse(temp[2]));

Results in the final output

2
-0.064262

and then an index out of range exception on the line “var temp2=vertices[k];” So apparently it can print vertices[k] (-0.064262) but when I go to assign it to another variable the array has suddenly shrunk.

Another way I tried to debug was to replace the last line with

if(modelPath.Contains("exec") && j>20000)
{
 print(vertices.length);
 print(lines[i+j]);
 print(vertices[0] + "," + vertices[1] + "," + vertices[2]);
 print (vertices[1]);
}
var temp = new Vector3(float.Parse(vertices[0]),0,0);

try
{
 temp.y=float.Parse(vertices[1]);
 temp.z=float.Parse(vertices[2]);
}
catch(e)
{
 print (vertices[1]);
}
objectMesh[j]=temp;

Which gives the output:

5
      -0.014949; 0.559054;-0.064262;;
-0.014949,0.559054,-0.064262
0.559054

then the line “print (vertices[1]);” inside the catch prints an empty string, and then there is a format exception again.

So basically, vertex[1] is equal to 0.559054 until it tries to parse it into the vector, and then suddenly it is null.

As you might imagine my attempts to debug have made me more confused than before, this behaviour is outside of my experience and I can’t make head or tail of it. Can anyone see anything I have missed?

The whole thing seems right; it’s time to shoot in the dark. Could the last line have some non-printable character lost anywhere? Edit the text file and re-write the coordinates to check this. Could the routine be waiting for an extra vertex? Try to add some extra values after the last line, like this:

  -0.014949; 0.559054;-0.064262;0;0;0;

The null string inside catch may mean nothing: maybe the system just emptied the stack before jumping to the error routine.

Another thing: what does the function cleanStrings? Could it be messing the string due to the extra ;?