[geos-commits] [SCM] GEOS branch master updated. eb5673a656c5f7f226b54246bbab6e0c8c64a37d

git at osgeo.org git at osgeo.org
Sun Sep 15 13:57:29 PDT 2019


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GEOS".

The branch, master has been updated
       via  eb5673a656c5f7f226b54246bbab6e0c8c64a37d (commit)
      from  a4aaa4e18dcb13b0804143d047e8b2d9b7c4aeeb (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit eb5673a656c5f7f226b54246bbab6e0c8c64a37d
Author: Daniel Baston <dbaston at gmail.com>
Date:   Sun Sep 15 16:57:19 2019 -0400

    Avoid heap alloc in EdgeRing::toPolygon
    
    Also avoid memory leak in case of exception.

diff --git a/include/geos/geomgraph/EdgeRing.h b/include/geos/geomgraph/EdgeRing.h
index d6d5167..63a0119 100644
--- a/include/geos/geomgraph/EdgeRing.h
+++ b/include/geos/geomgraph/EdgeRing.h
@@ -90,10 +90,9 @@ public:
 
     /**
      * Return a Polygon copying coordinates from this
-     * EdgeRing and its holes. Caller must remember
-     * to delete the result
+     * EdgeRing and its holes.
      */
-    geom::Polygon* toPolygon(const geom::GeometryFactory* geometryFactory);
+    std::unique_ptr<geom::Polygon> toPolygon(const geom::GeometryFactory* geometryFactory);
 
     /**
      * Compute a LinearRing from the point list previously collected.
@@ -122,7 +121,7 @@ public:
     bool containsPoint(const geom::Coordinate& p);
 
     void
-    testInvariant()
+    testInvariant() const
     {
         // pts are never NULL
         assert(pts);
diff --git a/src/geomgraph/EdgeRing.cpp b/src/geomgraph/EdgeRing.cpp
index e6e54c9..f76207a 100644
--- a/src/geomgraph/EdgeRing.cpp
+++ b/src/geomgraph/EdgeRing.cpp
@@ -36,6 +36,7 @@
 #include <geos/geom/LinearRing.h>
 #include <geos/geom/Location.h>
 #include <geos/geom/Envelope.h>
+#include <geos/util.h>
 
 #include <vector>
 #include <cassert>
@@ -159,23 +160,26 @@ EdgeRing::addHole(EdgeRing* edgeRing)
 }
 
 /*public*/
-Polygon*
+std::unique_ptr<Polygon>
 EdgeRing::toPolygon(const GeometryFactory* p_geometryFactory)
 {
     testInvariant();
 
-    size_t nholes = holes.size();
-    vector<LinearRing*>* holeLR = new vector<LinearRing*>(nholes);
-    for(size_t i = 0; i < nholes; ++i) {
-        (*holeLR)[i] = new LinearRing(*(holes[i]->getLinearRing()));
-    }
-
     // We don't use "clone" here because
     // GeometryFactory::createPolygon really
     // wants a LinearRing
-    //
-    LinearRing* shellLR = new LinearRing(*(getLinearRing()));
-    return p_geometryFactory->createPolygon(shellLR, holeLR);
+    auto shellLR = detail::make_unique<LinearRing>(*(getLinearRing()));
+    if (holes.empty()) {
+        return p_geometryFactory->createPolygon(std::move(shellLR));
+    } else {
+        size_t nholes = holes.size();
+        std::vector<std::unique_ptr<LinearRing>> holeLR(nholes);
+        for(size_t i = 0; i < nholes; ++i) {
+            holeLR[i] = detail::make_unique<LinearRing>(*(holes[i]->getLinearRing()));
+        }
+
+        return p_geometryFactory->createPolygon(std::move(shellLR), std::move(holeLR));
+    }
 }
 
 /*public*/
diff --git a/src/operation/overlay/PolygonBuilder.cpp b/src/operation/overlay/PolygonBuilder.cpp
index 551c7e1..979ad82 100644
--- a/src/operation/overlay/PolygonBuilder.cpp
+++ b/src/operation/overlay/PolygonBuilder.cpp
@@ -371,7 +371,7 @@ PolygonBuilder::computePolygons(vector<EdgeRing*>& newShellList)
     // add Polygons for all shells
     for(size_t i = 0, n = newShellList.size(); i < n; i++) {
         EdgeRing* er = newShellList[i];
-        Polygon* poly = er->toPolygon(geometryFactory);
+        Polygon* poly = er->toPolygon(geometryFactory).release();
         resultPolyList->push_back(poly);
     }
     return resultPolyList;

-----------------------------------------------------------------------

Summary of changes:
 include/geos/geomgraph/EdgeRing.h        |  7 +++----
 src/geomgraph/EdgeRing.cpp               | 24 ++++++++++++++----------
 src/operation/overlay/PolygonBuilder.cpp |  2 +-
 3 files changed, 18 insertions(+), 15 deletions(-)


hooks/post-receive
-- 
GEOS


More information about the geos-commits mailing list