I'm about 99% sure that you have some memory leaks in b2Ploygon
in this function:
Code:
void b2Polygon::MergeParallelEdges(float32 tolerance) {
if (nVertices <= 3) return;
bool* mergeMe = new bool[nVertices];
int32 newNVertices = nVertices;
for (int32 i = 0; i < nVertices; ++i) {
int32 lower = (i == 0) ? (nVertices - 1) : (i - 1);
int32 middle = i;
int32 upper = (i == nVertices - 1) ? (0) : (i + 1);
float32 dx0 = x[middle] - x[lower];
float32 dy0 = y[middle] - y[lower];
float32 dx1 = x[upper] - x[middle];
float32 dy1 = y[upper] - y[middle];
float32 norm0 = sqrtf(dx0*dx0+dy0*dy0);
float32 norm1 = sqrtf(dx1*dx1+dy1*dy1);
b2Assert(norm0 > 0.0f && norm1 > 0.0f);
dx0 /= norm0; dy0 /= norm0;
dx1 /= norm1; dy1 /= norm1;
float32 cross = dx0 * dy1 - dx1 * dy0;
if (fabs(cross) < tolerance) {
mergeMe[i] = true;
--newNVertices;
} else {
mergeMe[i] = false;
}
}
if(newNVertices == nVertices) return;
float32* newx = new float32[newNVertices];
float32* newy = new float32[newNVertices];
int32 currIndex = 0;
for (int32 i=0; i < nVertices; ++i) {
if (mergeMe[i]) continue;
newx[currIndex] = x[i];
newy[currIndex] = y[i];
++currIndex;
}
delete[] x;
delete[] y;
delete[] mergeMe;
x = newx;
y = newy;
nVertices = newNVertices;
}
I'm not sure if its something I'm doing incorrectly but the statement "if(newNVertices == nVertices) return;" can cause the bool array (mergeMe) thats newed to skip deletion.
and in this function: (sorry its long so I hope I get this explained well)
Code:
int32 PolygonizeTriangles(b2Triangle* triangulated, int32 triangulatedLength, b2Polygon* polys, int32 polysLength) {
int32 polyIndex = 0;
if (triangulatedLength == 0) {
return 0;
}
else {
int* covered = new int[triangulatedLength];
for (int32 i = 0; i < triangulatedLength; ++i) {
covered[i] = 0;
}
bool notDone = true;
while (notDone) {
int32 currTri = -1;
for (int32 i = 0; i < triangulatedLength; ++i) {
if (covered[i])
continue;
currTri = i;
break;
}
if (currTri == -1) {
notDone = false;
}
else {
b2Polygon poly(triangulated[currTri]);
covered[currTri] = 1;
for (int32 i = 0; i < triangulatedLength; i++) {
if (covered[i]) {
continue;
}
b2Polygon* newP = poly.Add(triangulated[i]);
if (!newP) {
continue;
}
if (newP->nVertices > b2Polygon::maxVerticesPerPolygon) {
delete newP;
continue;
}
if (newP->IsConvex()) {
poly.Set(*newP);
delete newP;
covered[i] = 1;
}
}
if (polyIndex < polysLength) polys[polyIndex].Set(poly);
polyIndex++;
}
}
delete[] covered;
}
int32 upperLim = (polyIndex > polysLength)?polysLength:polyIndex;
for (int32 i=0; i < upperLim; ++i) {
polys[i].MergeParallelEdges(FLT_EPSILON);
}
return polyIndex;
}
In the part where you have b2Polygon *newP = poly.Add(...) you only delete newP if newP->nVertices > b2Polygon::maxVerticesPerPolygon or if newP is convex. So say neither of those cases are true the next time through the loop you are setting newP = to a new polygon but not deleteing the old one. Again, I may be doing something wrong but I don't think so.
Let me know if you need more info or anything else.