[geos-commits] r3464 - in branches/3.3: src/algorithm tests/unit tests/unit/algorithm

svn_geos at osgeo.org svn_geos at osgeo.org
Mon Sep 12 03:39:19 EDT 2011


Author: strk
Date: 2011-09-12 00:39:19 -0700 (Mon, 12 Sep 2011)
New Revision: 3464

Added:
   branches/3.3/tests/unit/algorithm/InteriorPointAreaTest.cpp
Modified:
   branches/3.3/src/algorithm/InteriorPointArea.cpp
   branches/3.3/tests/unit/Makefile.am
Log:
Fix memory leak on invalid geometry in InteriorPointArea (#475)


Modified: branches/3.3/src/algorithm/InteriorPointArea.cpp
===================================================================
--- branches/3.3/src/algorithm/InteriorPointArea.cpp	2011-08-24 14:55:53 UTC (rev 3463)
+++ branches/3.3/src/algorithm/InteriorPointArea.cpp	2011-09-12 07:39:19 UTC (rev 3464)
@@ -1,9 +1,9 @@
 /**********************************************************************
- * $Id$
  *
  * GEOS - Geometry Engine Open Source
  * http://geos.refractions.net
  *
+ * Copyright (C) 2011      Sandro Santilli <strk at keybit.net>
  * Copyright (C) 2005-2006 Refractions Research Inc.
  * Copyright (C) 2001-2002 Vivid Solutions Inc.
  *
@@ -26,6 +26,7 @@
 
 #include <vector>
 #include <typeinfo>
+#include <memory> // for auto_ptr
 
 using namespace std;
 using namespace geos::geom;
@@ -88,19 +89,16 @@
 void
 InteriorPointArea::addPolygon(const Geometry *geometry)
 {
-	LineString *bisector=horizontalBisector(geometry);
-	Geometry *intersections=bisector->intersection(geometry);
-	const Geometry *widestIntersection=widestGeometry(intersections);
-	const Envelope *env=widestIntersection->getEnvelopeInternal();
-	double width=env->getWidth();
-	if (!foundInterior || width>maxWidth) {
-		env->centre(interiorPoint);
-		maxWidth = width;
-		foundInterior=true;
-	}
-	//delete env;
-	delete bisector;
-	delete intersections;
+  auto_ptr<LineString> bisector ( horizontalBisector(geometry) );
+  auto_ptr<Geometry> intersections ( bisector->intersection(geometry) );
+  const Geometry *widestIntersection = widestGeometry(intersections.get());
+  const Envelope *env = widestIntersection->getEnvelopeInternal();
+  double width=env->getWidth();
+  if (!foundInterior || width>maxWidth) {
+    env->centre(interiorPoint);
+    maxWidth = width;
+    foundInterior=true;
+  }
 }
 
 //@return if geometry is a collection, the widest sub-geometry; otherwise,
@@ -148,12 +146,9 @@
 	(*cv)[1].x = envelope->getMaxX();
 	(*cv)[1].y = avgY;
 
-	//delete envelope;
-
 	CoordinateSequence *cl = factory->getCoordinateSequenceFactory()->create(cv);
 
 	LineString *ret = factory->createLineString(cl);
-	//delete cl;
 	return ret;
 }
 

Modified: branches/3.3/tests/unit/Makefile.am
===================================================================
--- branches/3.3/tests/unit/Makefile.am	2011-08-24 14:55:53 UTC (rev 3463)
+++ branches/3.3/tests/unit/Makefile.am	2011-09-12 07:39:19 UTC (rev 3464)
@@ -32,6 +32,7 @@
 geos_unit_SOURCES = \
 	geos_unit.cpp \
 	algorithm/AngleTest.cpp \
+	algorithm/InteriorPointAreaTest.cpp \
 	algorithm/CGAlgorithms/isCCWTest.cpp \
 	algorithm/CGAlgorithms/isPointInRingTest.cpp \
 	algorithm/CGAlgorithms/computeOrientationTest.cpp \

Added: branches/3.3/tests/unit/algorithm/InteriorPointAreaTest.cpp
===================================================================
--- branches/3.3/tests/unit/algorithm/InteriorPointAreaTest.cpp	                        (rev 0)
+++ branches/3.3/tests/unit/algorithm/InteriorPointAreaTest.cpp	2011-09-12 07:39:19 UTC (rev 3464)
@@ -0,0 +1,70 @@
+// 
+// Test Suite for geos::algorithm::InteriorPointArea 
+
+#include <tut.hpp>
+// geos
+#include <geos/geom/Coordinate.h>
+#include <geos/algorithm/InteriorPointArea.h>
+#include <geos/io/WKTReader.h>
+#include <geos/geom/Geometry.h>
+// std
+#include <sstream>
+#include <string>
+#include <memory>
+
+namespace tut
+{
+    //
+    // Test Group
+    //
+
+    // dummy data, not used
+    struct test_interiorpointarea_data {
+      typedef geos::geom::Geometry Geometry;
+      typedef geos::geom::Coordinate Coordinate;
+      typedef geos::algorithm::InteriorPointArea InteriorPointArea;
+
+      geos::io::WKTReader reader;
+      std::auto_ptr<Geometry> geom;
+
+      test_interiorpointarea_data()
+      {}
+
+    };
+
+    typedef test_group<test_interiorpointarea_data> group;
+    typedef group::object object;
+
+    group test_interiorpointarea_group("geos::algorithm::InteriorPointArea");
+
+    //
+    // Test Cases
+    //
+
+    // http://trac.osgeo.org/geos/ticket/475
+    // This is a test for a memory leak more than anything else
+    template<>
+    template<>
+    void object::test<1>()
+    {
+      Coordinate centroid;
+
+      // this polygon is a typical hourglass-shape with a self intersection
+      // without a node
+      geom.reset( reader.read("POLYGON((6 54, 15 54, 6 47, 15 47, 6 54))") );
+
+      bool threw = false;
+
+      try {
+        InteriorPointArea interior_builder(geom.get());
+        interior_builder.getInteriorPoint(centroid);
+      }
+      catch(...) {
+        threw = true;
+      }
+
+      ensure(threw);
+    }
+
+} // namespace tut
+



More information about the geos-commits mailing list