Skip to content

Commit

Permalink
GeometryGraphOperation: Avoid leak when GeometryGraph ctor throws
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Mar 25, 2024
1 parent d12e2e6 commit 04e03d1
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 41 deletions.
3 changes: 2 additions & 1 deletion include/geos/geomgraph/DirectedEdgeStar.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#pragma once

#include <memory>
#include <geos/export.h>
#include <set>
#include <string>
Expand Down Expand Up @@ -81,7 +82,7 @@ class GEOS_DLL DirectedEdgeStar: public EdgeEndStar {
/** \brief
* Compute the labelling for all dirEdges in this star, as well as the overall labelling
*/
void computeLabelling(std::vector<GeometryGraph*>* geom) override; // throw(TopologyException *);
void computeLabelling(const std::vector<std::unique_ptr<GeometryGraph>>&geom) override; // throw(TopologyException *);

/** \brief
* For each dirEdge in the star, merge the label from the sym dirEdge into the label
Expand Down
7 changes: 4 additions & 3 deletions include/geos/geomgraph/EdgeEndStar.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <geos/geom/Coordinate.h> // for p0,p1

#include <array>
#include <memory>
#include <set>
#include <string>
#include <vector>
Expand Down Expand Up @@ -115,7 +116,7 @@ class GEOS_DLL EdgeEndStar {

virtual EdgeEnd* getNextCW(EdgeEnd* ee);

virtual void computeLabelling(std::vector<GeometryGraph*>* geomGraph);
virtual void computeLabelling(const std::vector<std::unique_ptr<GeometryGraph>>&geomGraph);
// throw(TopologyException *);

virtual bool isAreaLabelsConsistent(const GeometryGraph& geomGraph);
Expand Down Expand Up @@ -148,8 +149,8 @@ class GEOS_DLL EdgeEndStar {
private:

virtual geom::Location getLocation(uint32_t geomIndex,
const geom::Coordinate& p,
std::vector<GeometryGraph*>* geom);
const geom::Coordinate&p,
const std::vector<std::unique_ptr<GeometryGraph>>&geom);

/** \brief
* The location of the point for this star in
Expand Down
2 changes: 1 addition & 1 deletion include/geos/operation/GeometryGraphOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class GEOS_DLL GeometryGraphOperation {
/** \brief
* The operation args into an array so they can be accessed by index
*/
std::vector<geomgraph::GeometryGraph*> arg;
std::vector<std::unique_ptr<geomgraph::GeometryGraph>> arg;

void setComputationPrecision(const geom::PrecisionModel* pm);
};
Expand Down
4 changes: 2 additions & 2 deletions include/geos/operation/relate/RelateComputer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace relate { // geos::operation::relate
*/
class GEOS_DLL RelateComputer {
public:
RelateComputer(std::vector<geomgraph::GeometryGraph*>* newArg);
RelateComputer(std::vector<std::unique_ptr<geomgraph::GeometryGraph>>& newArg);
~RelateComputer() = default;

std::unique_ptr<geom::IntersectionMatrix> computeIM();
Expand All @@ -87,7 +87,7 @@ class GEOS_DLL RelateComputer {
algorithm::PointLocator ptLocator;

/// the arg(s) of the operation
std::vector<geomgraph::GeometryGraph*>* arg;
const std::vector<std::unique_ptr<geomgraph::GeometryGraph>>& arg;

geomgraph::NodeMap nodes;

Expand Down
2 changes: 1 addition & 1 deletion src/geomgraph/DirectedEdgeStar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ DirectedEdgeStar::getRightmostEdge()

/*public*/
void
DirectedEdgeStar::computeLabelling(std::vector<GeometryGraph*>* geom)
DirectedEdgeStar::computeLabelling(const std::vector<std::unique_ptr<GeometryGraph>>&geom)
//throw(TopologyException *)
{
// this call can throw a TopologyException
Expand Down
8 changes: 4 additions & 4 deletions src/geomgraph/EdgeEndStar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ EdgeEndStar::getNextCW(EdgeEnd* ee)

/*public*/
void
EdgeEndStar::computeLabelling(std::vector<GeometryGraph*>* geomGraph)
EdgeEndStar::computeLabelling(const std::vector<std::unique_ptr<GeometryGraph>>& geomGraph)
//throw(TopologyException *)
{
computeEdgeEndLabels((*geomGraph)[0]->getBoundaryNodeRule());
computeEdgeEndLabels(geomGraph[0]->getBoundaryNodeRule());

// Propagate side labels around the edges in the star
// for each parent Geometry
Expand Down Expand Up @@ -185,12 +185,12 @@ EdgeEndStar::computeEdgeEndLabels(
/*public*/
Location
EdgeEndStar::getLocation(uint32_t geomIndex,
const Coordinate& p, std::vector<GeometryGraph*>* geom)
const Coordinate& p, const std::vector<std::unique_ptr<GeometryGraph>>& geom)
{
// compute location only on demand
if(ptInAreaLocation[geomIndex] == Location::NONE) {
ptInAreaLocation[geomIndex] = algorithm::locate::SimplePointInAreaLocator::locate(p,
(*geom)[geomIndex]->getGeometry());
geom[geomIndex]->getGeometry());
}
return ptInAreaLocation[geomIndex];
}
Expand Down
13 changes: 5 additions & 8 deletions src/operation/GeometryGraphOperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ GeometryGraphOperation::GeometryGraphOperation(const Geometry* g0,
setComputationPrecision(pm1);
}

arg[0] = new GeometryGraph(0, g0,
arg[0] = std::make_unique<GeometryGraph>(0, g0,
algorithm::BoundaryNodeRule::getBoundaryOGCSFS());
arg[1] = new GeometryGraph(1, g1,
arg[1] = std::make_unique<GeometryGraph>(1, g1,
algorithm::BoundaryNodeRule::getBoundaryOGCSFS());
}

Expand All @@ -80,8 +80,8 @@ GeometryGraphOperation::GeometryGraphOperation(const Geometry* g0,
setComputationPrecision(pm1);
}

arg[0] = new GeometryGraph(0, g0, boundaryNodeRule);
arg[1] = new GeometryGraph(1, g1, boundaryNodeRule);
arg[0] = std::make_unique<GeometryGraph>(0, g0, boundaryNodeRule);
arg[1] = std::make_unique<GeometryGraph>(1, g1, boundaryNodeRule);
}


Expand All @@ -93,7 +93,7 @@ GeometryGraphOperation::GeometryGraphOperation(const Geometry* g0):

setComputationPrecision(pm0);

arg[0] = new GeometryGraph(0, g0);
arg[0] = std::make_unique<GeometryGraph>(0, g0);
}

const Geometry*
Expand All @@ -114,9 +114,6 @@ GeometryGraphOperation::setComputationPrecision(const PrecisionModel* pm)

GeometryGraphOperation::~GeometryGraphOperation()
{
for(unsigned int i = 0; i < arg.size(); ++i) {
delete arg[i];
}
}

} // namespace geos.operation
Expand Down
38 changes: 19 additions & 19 deletions src/operation/relate/RelateComputer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace geos {
namespace operation { // geos.operation
namespace relate { // geos.operation.relate

RelateComputer::RelateComputer(std::vector<GeometryGraph*>* newArg):
RelateComputer::RelateComputer(std::vector<std::unique_ptr<GeometryGraph>>& newArg):
arg(newArg),
nodes(RelateNodeFactory::instance()),
im(new IntersectionMatrix())
Expand All @@ -74,10 +74,10 @@ RelateComputer::computeIM()
// since Geometries are finite and embedded in a 2-D space, the EE element must always be 2
im->set(Location::EXTERIOR, Location::EXTERIOR, 2);
// if the Geometries don't overlap there is nothing to do
const Envelope* e1 = (*arg)[0]->getGeometry()->getEnvelopeInternal();
const Envelope* e2 = (*arg)[1]->getGeometry()->getEnvelopeInternal();
const Envelope* e1 = arg[0]->getGeometry()->getEnvelopeInternal();
const Envelope* e2 = arg[1]->getGeometry()->getEnvelopeInternal();
if(!e1->intersects(e2)) {
computeDisjointIM(im.get(), (*arg)[0]->getBoundaryNodeRule());
computeDisjointIM(im.get(), arg[0]->getBoundaryNodeRule());
return std::move(im);
}

Expand All @@ -88,7 +88,7 @@ RelateComputer::computeIM()
#endif

std::unique_ptr<SegmentIntersector> si1(
(*arg)[0]->computeSelfNodes(&li, false)
arg[0]->computeSelfNodes(&li, false)
);

GEOS_CHECK_FOR_INTERRUPTS();
Expand All @@ -100,7 +100,7 @@ RelateComputer::computeIM()
#endif

std::unique_ptr<SegmentIntersector> si2(
(*arg)[1]->computeSelfNodes(&li, false)
arg[1]->computeSelfNodes(&li, false)
);

GEOS_CHECK_FOR_INTERRUPTS();
Expand All @@ -113,7 +113,7 @@ RelateComputer::computeIM()

// compute intersections between edges of the two input geometries
std::unique_ptr< SegmentIntersector> intersector(
(*arg)[0]->computeEdgeIntersections((*arg)[1], &li, false)
arg[0]->computeEdgeIntersections(arg[1].get(), &li, false)
);

GEOS_CHECK_FOR_INTERRUPTS();
Expand Down Expand Up @@ -188,9 +188,9 @@ RelateComputer::computeIM()
*/
// build EdgeEnds for all intersections
EdgeEndBuilder eeBuilder;
auto&& ee0 = eeBuilder.computeEdgeEnds((*arg)[0]->getEdges());
auto&& ee0 = eeBuilder.computeEdgeEnds(arg[0]->getEdges());
insertEdgeEnds(ee0);
auto&& ee1 = eeBuilder.computeEdgeEnds((*arg)[1]->getEdges());
auto&& ee1 = eeBuilder.computeEdgeEnds(arg[1]->getEdges());

#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
Expand Down Expand Up @@ -246,8 +246,8 @@ void
RelateComputer::computeProperIntersectionIM(SegmentIntersector* intersector, IntersectionMatrix* imX)
{
// If a proper intersection is found, we can set a lower bound on the IM.
int dimA = (*arg)[0]->getGeometry()->getDimension();
int dimB = (*arg)[1]->getGeometry()->getDimension();
int dimA = arg[0]->getGeometry()->getDimension();
int dimB = arg[1]->getGeometry()->getDimension();
bool hasProper = intersector->hasProperIntersection();
bool hasProperInterior = intersector->hasProperInteriorIntersection();
// For Geometry's of dim 0 there can never be proper intersections.
Expand Down Expand Up @@ -311,7 +311,7 @@ RelateComputer::computeProperIntersectionIM(SegmentIntersector* intersector, Int
void
RelateComputer::copyNodesAndLabels(uint8_t argIndex)
{
const NodeMap* nm = (*arg)[argIndex]->getNodeMap();
const NodeMap* nm = arg[argIndex]->getNodeMap();
for(const auto& it: *nm) {
const Node* graphNode = it.second.get();
Node* newNode = nodes.addNode(graphNode->getCoordinate());
Expand All @@ -333,7 +333,7 @@ RelateComputer::copyNodesAndLabels(uint8_t argIndex)
void
RelateComputer::computeIntersectionNodes(uint8_t argIndex)
{
std::vector<Edge*>* edges = (*arg)[argIndex]->getEdges();
std::vector<Edge*>* edges = arg[argIndex]->getEdges();
for(Edge* e: *edges) {
Location eLoc = e->getLabel().getLocation(argIndex);
const EdgeIntersectionList& eiL = e->getEdgeIntersectionList();
Expand Down Expand Up @@ -361,7 +361,7 @@ RelateComputer::computeIntersectionNodes(uint8_t argIndex)
void
RelateComputer::labelIntersectionNodes(uint8_t argIndex)
{
std::vector<Edge*>* edges = (*arg)[argIndex]->getEdges();
std::vector<Edge*>* edges = arg[argIndex]->getEdges();
for(Edge* e: *edges) {
Location eLoc = e->getLabel().getLocation(argIndex);
EdgeIntersectionList& eiL = e->getEdgeIntersectionList();
Expand All @@ -384,12 +384,12 @@ RelateComputer::labelIntersectionNodes(uint8_t argIndex)
void
RelateComputer::computeDisjointIM(IntersectionMatrix* imX, const algorithm::BoundaryNodeRule& boundaryNodeRule)
{
const Geometry* ga = (*arg)[0]->getGeometry();
const Geometry* ga = arg[0]->getGeometry();
if(!ga->isEmpty()) {
imX->set(Location::INTERIOR, Location::EXTERIOR, ga->getDimension());
imX->set(Location::BOUNDARY, Location::EXTERIOR, getBoundaryDim(*ga, boundaryNodeRule));
}
const Geometry* gb = (*arg)[1]->getGeometry();
const Geometry* gb = arg[1]->getGeometry();
if(!gb->isEmpty()) {
imX->set(Location::EXTERIOR, Location::INTERIOR, gb->getDimension());
imX->set(Location::EXTERIOR, Location::BOUNDARY, getBoundaryDim(*gb, boundaryNodeRule));
Expand Down Expand Up @@ -453,10 +453,10 @@ RelateComputer::updateIM(IntersectionMatrix& imX)
void
RelateComputer::labelIsolatedEdges(uint8_t thisIndex, uint8_t targetIndex)
{
std::vector<Edge*>* edges = (*arg)[thisIndex]->getEdges();
std::vector<Edge*>* edges = arg[thisIndex]->getEdges();
for(Edge* e: *edges) {
if(e->isIsolated()) {
labelIsolatedEdge(e, targetIndex, (*arg)[targetIndex]->getGeometry());
labelIsolatedEdge(e, targetIndex, arg[targetIndex]->getGeometry());
isolatedEdges.push_back(e);
}
}
Expand Down Expand Up @@ -505,7 +505,7 @@ void
RelateComputer::labelIsolatedNode(Node* n, uint8_t targetIndex)
{
Location loc = ptLocator.locate(n->getCoordinate(),
(*arg)[targetIndex]->getGeometry());
arg[targetIndex]->getGeometry());
n->getLabel().setAllLocations(targetIndex, loc);
//debugPrintln(n.getLabel());
}
Expand Down
4 changes: 2 additions & 2 deletions src/operation/relate/RelateOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ RelateOp::relate(const Geometry* a, const Geometry* b,

RelateOp::RelateOp(const Geometry* g0, const Geometry* g1):
GeometryGraphOperation(g0, g1),
relateComp(&arg)
relateComp(arg)
{
}

RelateOp::RelateOp(const Geometry* g0, const Geometry* g1,
const algorithm::BoundaryNodeRule& boundaryNodeRule)
:
GeometryGraphOperation(g0, g1, boundaryNodeRule),
relateComp(&arg)
relateComp(arg)
{
}

Expand Down

0 comments on commit 04e03d1

Please sign in to comment.