From dbcd1e2df645b1e2f5cb597ac0bb135ef6730aae Mon Sep 17 00:00:00 2001 From: ntfshard Date: Sat, 1 Aug 2015 14:53:21 +0300 Subject: [PATCH] Fix: memory leak in ExPolygon::triangulate_p2t --- xs/src/libslic3r/ExPolygon.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/xs/src/libslic3r/ExPolygon.cpp b/xs/src/libslic3r/ExPolygon.cpp index ee0473656..c423c6e0a 100644 --- a/xs/src/libslic3r/ExPolygon.cpp +++ b/xs/src/libslic3r/ExPolygon.cpp @@ -375,31 +375,29 @@ ExPolygon::triangulate_p2t(Polygons* polygons) const simplify_polygons(*this, &expp, true); for (ExPolygons::const_iterator ex = expp.begin(); ex != expp.end(); ++ex) { - p2t::CDT* cdt; - // TODO: prevent duplicate points - + // contour - { - std::vector points; - for (Points::const_iterator point = ex->contour.points.begin(); point != ex->contour.points.end(); ++point) { - points.push_back(new p2t::Point(point->x, point->y)); - } - cdt = new p2t::CDT(points); + std::vector ContourPoints; + for (Points::const_iterator point = ex->contour.points.begin(); point != ex->contour.points.end(); ++point) { + // We should delete each p2t::Point object + ContourPoints.push_back(new p2t::Point(point->x, point->y)); } - + p2t::CDT cdt(ContourPoints); + // holes for (Polygons::const_iterator hole = ex->holes.begin(); hole != ex->holes.end(); ++hole) { std::vector points; for (Points::const_iterator point = hole->points.begin(); point != hole->points.end(); ++point) { + // will be destructed in SweepContext::~SweepContext points.push_back(new p2t::Point(point->x, point->y)); } - cdt->AddHole(points); + cdt.AddHole(points); } // perform triangulation - cdt->Triangulate(); - std::vector triangles = cdt->GetTriangles(); + cdt.Triangulate(); + std::vector triangles = cdt.GetTriangles(); for (std::vector::const_iterator triangle = triangles.begin(); triangle != triangles.end(); ++triangle) { Polygon p; @@ -409,6 +407,10 @@ ExPolygon::triangulate_p2t(Polygons* polygons) const } polygons->push_back(p); } + + for(std::vector::iterator it = ContourPoints.begin(); it != ContourPoints.end(); ++it) { + delete *it; + } } }