[geos-commits] r4104 - trunk/src/geomgraph

svn_geos at osgeo.org svn_geos at osgeo.org
Mon Oct 5 02:38:40 PDT 2015


Author: mloskot
Date: 2015-10-05 02:38:40 -0700 (Mon, 05 Oct 2015)
New Revision: 4104

Modified:
   trunk/src/geomgraph/PlanarGraph.cpp
Log:
Use std::auto_ptr to simplify r4100 fix of a memory leak potential

Modified: trunk/src/geomgraph/PlanarGraph.cpp
===================================================================
--- trunk/src/geomgraph/PlanarGraph.cpp	2015-10-04 16:09:41 UTC (rev 4103)
+++ trunk/src/geomgraph/PlanarGraph.cpp	2015-10-05 09:38:40 UTC (rev 4104)
@@ -133,6 +133,8 @@
 void
 PlanarGraph::add(EdgeEnd* e)
 {
+	// It is critical to add the edge to the edgeEndList first,
+	// then it is safe to follow with any potentially throwing operations.
 	assert(edgeEndList);
 	edgeEndList->push_back(e);
 
@@ -210,23 +212,14 @@
 		// PlanarGraph destructor will delete all DirectedEdges 
 		// in edgeEndList, which is where these are added
 		// by the ::add(EdgeEnd) call
-		DirectedEdge *de1=new DirectedEdge(e, true);
-		DirectedEdge *de2=new DirectedEdge(e, false);
-		de1->setSym(de2);
-		de2->setSym(de1);
+		std::auto_ptr<DirectedEdge> de1(new DirectedEdge(e, true));
+		std::auto_ptr<DirectedEdge> de2(new DirectedEdge(e, false));
+		de1->setSym(de2.get());
+		de2->setSym(de1.get());
 
-		// ::add may throw, then de2 will not be registered for deletion
-		try
-		{
-			add(de1);
-		}
-		catch (...)
-		{
-			delete de2;
-			// no need to delete de1 which has already been registered in edgeEndList
-			throw;
-		}
-		add(de2);
+		// First, ::add takes the ownership, then follows with operations that may throw.
+		add(de1.release());
+		add(de2.release());
 	}
 }
 



More information about the geos-commits mailing list