From b7943e7420f37027a332d3ab0ba7eda967c05687 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Mon, 31 Oct 2016 22:27:50 +0000 Subject: [PATCH] Fix memory leak in IndexedFacetDistance Patch by Dan Baston (see #795) git-svn-id: http://svn.osgeo.org/geos/trunk@4300 5242fede-7e19-0410-aef8-94bd7d2200fb --- .../operation/distance/IndexedFacetDistance.h | 2 ++ src/operation/distance/IndexedFacetDistance.cpp | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/geos/operation/distance/IndexedFacetDistance.h b/include/geos/operation/distance/IndexedFacetDistance.h index 025c9a5033..a819662850 100644 --- a/include/geos/operation/distance/IndexedFacetDistance.h +++ b/include/geos/operation/distance/IndexedFacetDistance.h @@ -34,6 +34,8 @@ namespace geos { double getDistance(const geom::Geometry * g) const; + ~IndexedFacetDistance(); + private: std::auto_ptr cachedTree; diff --git a/src/operation/distance/IndexedFacetDistance.cpp b/src/operation/distance/IndexedFacetDistance.cpp index db4a7e189a..fa22bb3019 100644 --- a/src/operation/distance/IndexedFacetDistance.cpp +++ b/src/operation/distance/IndexedFacetDistance.cpp @@ -18,6 +18,7 @@ #include #include +#include using namespace geos::geom; using namespace geos::index::strtree; @@ -25,6 +26,12 @@ using namespace geos::index::strtree; namespace geos { namespace operation { namespace distance { + struct : public index::ItemVisitor { + void visitItem(void * item) { + delete static_cast(item); + } + } deleter; + double IndexedFacetDistance::distance(const Geometry * g1, const Geometry * g2) { IndexedFacetDistance ifd(g1); return ifd.getDistance(g2); @@ -41,7 +48,15 @@ namespace geos { std::pair obj = cachedTree->nearestNeighbour(tree2.get(), dynamic_cast(&itemDistance)); - return static_cast(obj.first)->distance(*static_cast(obj.second)); + double distance = static_cast(obj.first)->distance(*static_cast(obj.second)); + + tree2->iterate(deleter); + + return distance; + } + + IndexedFacetDistance::~IndexedFacetDistance() { + cachedTree->iterate(deleter); } } }