EXC_BAD_ACCESS crash (when too many hills are visible?)
Closed this issue · 2 comments
It crashes on this line: hillVertices[nHillVertices] = (ccVertex2F){pt1.x, pt1.y-(float)textureSize/vSegments*k};
inside Terrain.mm
. When the debugger stops me at that line, nHillVertices
is some huge number like 1107978021, outside the bounds of the array.
I would have thought that this is related to dividing by 0, but k
is initialized to 0 in each iteration, and this crash only happens when I get high in the air and can see alot of hills, so I guess it's not that.
Here's a screenshot of how the game looks when it crashes:
Here's the surrounding code:
nHillVertices = 0;
ccVertex2F p0, p1, pt0, pt1;
p0 = hillKeyPoints[fromKeyPointI];
for (int i=fromKeyPointI+1; i<toKeyPointI+1; i++) {
p1 = hillKeyPoints[i];
// triangle strip between p0 and p1
int hSegments = floorf((p1.x-p0.x)/kHillSegmentWidth);
int vSegments = 1;
float dx = (p1.x - p0.x) / hSegments;
float da = M_PI / hSegments;
float ymid = (p0.y + p1.y) / 2;
float ampl = (p0.y - p1.y) / 2;
pt0 = p0;
for (int j=1; j<hSegments+1; j++) {
pt1.x = p0.x + j*dx;
pt1.y = ymid + ampl * cosf(da*j);
for (int k=0; k<vSegments+1; k++) {
hillVertices[nHillVertices] = (ccVertex2F){pt0.x, pt0.y-(float)textureSize/vSegments*k};
hillTexCoords[nHillVertices++] = (ccVertex2F){pt0.x/(float)textureSize, (float)(k)/vSegments};
hillVertices[nHillVertices] = (ccVertex2F){pt1.x, pt1.y-(float)textureSize/vSegments*k};
hillTexCoords[nHillVertices++] = (ccVertex2F){pt1.x/(float)textureSize, (float)(k)/vSegments};
}
pt0 = pt1;
}
p0 = p1;
}
Hmm I think this could be because nHillVertices
got beyond kMaxHillVertices
. I stepped that value up from 1k to 2k and noticed that nHillVertices
several times reached over 1k. I'm not super familiar with C, but would the problem have been that going beyond the bounds of hillTexCoords
ran over into the memory of nHillVertices
?
After stepping it up to 2k I haven't been able to replicate the crash.
Merged. Thanks!