diff --git a/include/qbsp/brush.hh b/include/qbsp/brush.hh index 5673292a..dfd35648 100644 --- a/include/qbsp/brush.hh +++ b/include/qbsp/brush.hh @@ -53,4 +53,6 @@ vec_t BrushVolume (const brush_t *brush); int BrushMostlyOnSide (const brush_t *brush, const vec3_t normal, vec_t dist); void SplitBrush (const brush_t *brush, int planenum, int planeside, brush_t **front, brush_t **back); +void FreeBrush(brush_t *brush); + #endif diff --git a/qbsp/brush.cc b/qbsp/brush.cc index 91c32321..b3dc9a89 100644 --- a/qbsp/brush.cc +++ b/qbsp/brush.cc @@ -514,8 +514,7 @@ FreeBrushes(mapentity_t *ent) for (brush = ent->brushes; brush; brush = next) { next = brush->next; - FreeBrushFaces(brush->faces); - FreeMem(brush, BRUSH, 1); + FreeBrush(brush); } ent->brushes = nullptr; } @@ -528,7 +527,7 @@ FreeBrush void FreeBrush(brush_t *brush) { - Q_assert(brush->next == nullptr); + FreeBrushFaces(brush->faces); FreeMem(brush, BRUSH, 1); } @@ -1076,7 +1075,7 @@ Brush_LoadEntity(mapentity_t *dst, const mapentity_t *src, const int hullnum) VectorCopy(origin, rotate_offset); usesOriginBrush = true; - FreeMem(brush, BRUSH, 1); + FreeBrush(brush); } } } @@ -1164,8 +1163,7 @@ Brush_LoadEntity(mapentity_t *dst, const mapentity_t *src, const int hullnum) if (brush) { AddToBounds(dst, brush->mins); AddToBounds(dst, brush->maxs); - FreeBrushFaces(brush->faces); - FreeMem(brush, BRUSH, 1); + FreeBrush(brush); } continue; } @@ -1572,6 +1570,11 @@ void SplitBrush (const brush_t *brush, newface->next = b[j]->faces; b[j]->faces = newface; } + + if (cw[0]) + FreeMem(cw[0], WINDING, 1); + if (cw[1]) + FreeMem(cw[1], WINDING, 1); } @@ -1651,7 +1654,7 @@ void SplitBrush (const brush_t *brush, v1 = BrushVolume (b[i]); if (v1 < 1.0) { - FreeMem (b[i], BRUSH, 1); + FreeBrush(b[i]); b[i] = nullptr; logprint ("tiny volume after clip\n"); } @@ -1660,5 +1663,7 @@ void SplitBrush (const brush_t *brush, *front = b[0]; *back = b[1]; + + FreeMem(midwinding, WINDING, 1); } diff --git a/qbsp/test_qbsp.cc b/qbsp/test_qbsp.cc index f6d63011..b55e39f5 100644 --- a/qbsp/test_qbsp.cc +++ b/qbsp/test_qbsp.cc @@ -118,7 +118,7 @@ TEST(qbsp, duplicatePlanes) { brush_t *brush = LoadBrush(&worldspawn.mapbrush(0), vec3_origin, 0); ASSERT_NE(nullptr, brush); EXPECT_EQ(6, Brush_NumFaces(brush)); - FreeMem(brush, BRUSH, 1); + FreeBrush(brush); } static brush_t *load128x128x32Brush() @@ -163,7 +163,7 @@ TEST(qbsp, BrushMostlyOnSide1) { EXPECT_EQ(SIDE_FRONT, BrushMostlyOnSide(brush, plane1normal, plane1dist)); - FreeMem(brush, BRUSH, 1); + FreeBrush(brush); } TEST(qbsp, BrushMostlyOnSide2) { @@ -174,7 +174,7 @@ TEST(qbsp, BrushMostlyOnSide2) { EXPECT_EQ(SIDE_BACK, BrushMostlyOnSide(brush, plane1normal, plane1dist)); - FreeMem(brush, BRUSH, 1); + FreeBrush(brush); } TEST(qbsp, BoundBrush) { @@ -192,7 +192,7 @@ TEST(qbsp, BoundBrush) { EXPECT_FLOAT_EQ(64, brush->maxs[1]); EXPECT_FLOAT_EQ(16, brush->maxs[2]); - FreeMem(brush, BRUSH, 1); + FreeBrush(brush); } static void checkForAllCubeNormals(const brush_t *brush) @@ -268,7 +268,7 @@ TEST(qbsp, SplitBrush) { checkCube(back); - FreeMem(brush, BRUSH, 1); + FreeBrush(brush); FreeMem(front, BRUSH, 1); FreeMem(back, BRUSH, 1); } @@ -289,6 +289,24 @@ TEST(qbsp, SplitBrushOnSide) { EXPECT_EQ(nullptr, back); } +#if 0 +TEST(qbsp, MemLeaks) { + brush_t *brush = load128x128x32Brush(); + + const vec3_t planenormal = { -1, 0, 0 }; + int planeside; + const int planenum = FindPlane(planenormal, 0.0, &planeside); + + for (int i=0; i<1000000; i++) { + brush_t *front, *back; + SplitBrush(brush, planenum, planeside, &front, &back); + + FreeBrush(front); + FreeBrush(back); + } +} +#endif + TEST(mathlib, WindingArea) { winding_t w; w.numpoints = 5;