Skip to content

Commit 4b78b1c

Browse files
committed
Make all QgsGeometry methods return values, not pointers
Reduces likelihood of crashes and leaks
1 parent bd7d913 commit 4b78b1c

File tree

103 files changed

+1158
-1562
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1158
-1562
lines changed

doc/api_break.dox

+9-1
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,18 @@ true, as the method will return an empty geometry if the feature has no geometry
226226
<li>setFields( const QgsFields*, bool ) has been removed, use setFields( const QgsFields&, bool ) instead.</li>
227227
</ul>
228228

229+
\subsection qgis_api_break_3_0_QgsGeometry QgsGeometry
230+
231+
<ul>
232+
<li>All QgsGeometry methods now accept geometry references instead of pointers, and return a QgsGeometry
233+
value instead of a pointer.</li>
234+
</ul>
235+
229236
\subsection qgis_api_break_3_0_QgsGeometryAnalyzer QgsGeometryAnalyzer
230237

231238
<ul>
232-
<li>locateBetweenMeasures() and locateAlongMeasure() now take geometry references, not pointers</li>
239+
<li>locateBetweenMeasures() and locateAlongMeasure() now take geometry references, not pointers, and return
240+
a QgsGeometry value rather than a pointer.</li>
233241
</ul>
234242

235243
\subsection qgis_api_break_3_0_QgsGroupWMSDataDialog QgsGroupWMSDataDialog

python/analysis/vector/qgsgeometryanalyzer.sip

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ class QgsGeometryAnalyzer
9595
bool forceSingleGeometry = false, QgsVectorDataProvider* memoryProvider = 0, QProgressDialog* p = 0 );
9696

9797
/** Returns linear reference geometry as a multiline (or 0 if no match). Currently, the z-coordinates are considered to be the measures (no support for m-values in QGIS)*/
98-
QgsGeometry* locateBetweenMeasures( double fromMeasure, double toMeasure, const QgsGeometry& lineGeom );
98+
QgsGeometry locateBetweenMeasures( double fromMeasure, double toMeasure, const QgsGeometry& lineGeom );
9999
/** Returns linear reference geometry. Unlike the PostGIS function, this method always returns multipoint or 0 if no match (not geometry collection).
100100
* Currently, the z-coordinates are considered to be the measures (no support for m-values in QGIS)
101101
*/
102-
QgsGeometry* locateAlongMeasure( double measure, const QgsGeometry& lineGeom );
102+
QgsGeometry locateAlongMeasure( double measure, const QgsGeometry& lineGeom );
103103

104104
};

python/core/geometry/qgsgeometry.sip

+36-34
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ class QgsGeometry
6565
bool isEmpty() const;
6666

6767
/** Creates a new geometry from a WKT string */
68-
static QgsGeometry* fromWkt( const QString& wkt ) /Factory/;
68+
static QgsGeometry fromWkt( const QString& wkt );
6969
/** Creates a new geometry from a QgsPoint object*/
70-
static QgsGeometry* fromPoint( const QgsPoint& point ) /Factory/;
70+
static QgsGeometry fromPoint( const QgsPoint& point );
7171
/** Creates a new geometry from a QgsMultiPoint object */
72-
static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint ) /Factory/;
72+
static QgsGeometry fromMultiPoint( const QgsMultiPoint& multipoint );
7373
/** Creates a new geometry from a QgsPolyline object */
74-
static QgsGeometry* fromPolyline( const QgsPolyline& polyline ) /Factory/;
74+
static QgsGeometry fromPolyline( const QgsPolyline& polyline );
7575
/** Creates a new geometry from a QgsMultiPolyline object*/
76-
static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline ) /Factory/;
76+
static QgsGeometry fromMultiPolyline( const QgsMultiPolyline& multiline );
7777
/** Creates a new geometry from a QgsPolygon */
78-
static QgsGeometry* fromPolygon( const QgsPolygon& polygon ) /Factory/;
78+
static QgsGeometry fromPolygon( const QgsPolygon& polygon );
7979
/** Creates a new geometry from a QgsMultiPolygon */
80-
static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly ) /Factory/;
80+
static QgsGeometry fromMultiPolygon( const QgsMultiPolygon& multipoly );
8181
/** Creates a new geometry from a QgsRectangle */
82-
static QgsGeometry* fromRect( const QgsRectangle& rect ) /Factory/;
82+
static QgsGeometry fromRect( const QgsRectangle& rect );
8383

8484
/**
8585
* Set the geometry, feeding in a geometry in GEOS format.
@@ -408,42 +408,42 @@ class QgsGeometry
408408
bool intersects( const QgsRectangle& r ) const;
409409

410410
/** Test for intersection with a geometry (uses GEOS) */
411-
bool intersects( const QgsGeometry* geometry ) const;
411+
bool intersects( const QgsGeometry& geometry ) const;
412412

413413
/** Test for containment of a point (uses GEOS) */
414414
bool contains( const QgsPoint* p ) const;
415415

416416
/** Test for if geometry is contained in another (uses GEOS)
417417
* @note added in 1.5 */
418-
bool contains( const QgsGeometry* geometry ) const;
418+
bool contains( const QgsGeometry& geometry ) const;
419419

420420
/** Test for if geometry is disjoint of another (uses GEOS)
421421
* @note added in 1.5 */
422-
bool disjoint( const QgsGeometry* geometry ) const;
422+
bool disjoint( const QgsGeometry& geometry ) const;
423423

424424
/** Test for if geometry equals another (uses GEOS)
425425
* @note added in 1.5 */
426-
bool equals( const QgsGeometry* geometry ) const;
426+
bool equals( const QgsGeometry& geometry ) const;
427427

428428
/** Test for if geometry touch another (uses GEOS)
429429
* @note added in 1.5 */
430-
bool touches( const QgsGeometry* geometry ) const;
430+
bool touches( const QgsGeometry& geometry ) const;
431431

432432
/** Test for if geometry overlaps another (uses GEOS)
433433
* @note added in 1.5 */
434-
bool overlaps( const QgsGeometry* geometry ) const;
434+
bool overlaps( const QgsGeometry& geometry ) const;
435435

436436
/** Test for if geometry is within another (uses GEOS)
437437
* @note added in 1.5 */
438-
bool within( const QgsGeometry* geometry ) const;
438+
bool within( const QgsGeometry& geometry ) const;
439439

440440
/** Test for if geometry crosses another (uses GEOS)
441441
* @note added in 1.5 */
442-
bool crosses( const QgsGeometry* geometry ) const;
442+
bool crosses( const QgsGeometry& geometry ) const;
443443

444444
/** Returns a buffer region around this geometry having the given width and with a specified number
445445
of segments used to approximate curves */
446-
QgsGeometry* buffer( double distance, int segments ) const /Factory/;
446+
QgsGeometry buffer( double distance, int segments ) const;
447447

448448
/** Returns a buffer region around the geometry, with additional style options.
449449
* @param distance buffer distance
@@ -454,44 +454,44 @@ class QgsGeometry
454454
* @note added in 2.4
455455
* @note needs GEOS >= 3.3 - otherwise always returns 0
456456
*/
457-
QgsGeometry* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit ) const /Factory/;
457+
QgsGeometry buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit ) const;
458458

459459
/** Returns an offset line at a given distance and side from an input line.
460460
* See buffer() method for details on parameters.
461461
* @note added in 2.4
462462
* @note needs GEOS >= 3.3 - otherwise always returns 0
463463
*/
464-
QgsGeometry* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit ) const /Factory/;
464+
QgsGeometry offsetCurve( double distance, int segments, int joinStyle, double mitreLimit ) const;
465465

466466
/** Returns a simplified version of this geometry using a specified tolerance value */
467-
QgsGeometry* simplify( double tolerance ) const /Factory/;
467+
QgsGeometry simplify( double tolerance ) const;
468468

469469
/** Returns the center of mass of a geometry
470470
* @note for line based geometries, the center point of the line is returned,
471471
* and for point based geometries, the point itself is returned
472472
*/
473-
QgsGeometry* centroid() const /Factory/;
473+
QgsGeometry centroid() const;
474474

475475
/** Returns a point within a geometry */
476-
QgsGeometry* pointOnSurface() const /Factory/;
476+
QgsGeometry pointOnSurface() const;
477477

478478
/** Returns the smallest convex polygon that contains all the points in the geometry. */
479-
QgsGeometry* convexHull() const /Factory/;
479+
QgsGeometry convexHull() const;
480480

481481
/**
482482
* Return interpolated point on line at distance
483483
* @note added in 1.9
484484
*/
485-
QgsGeometry* interpolate( double distance ) /Factory/;
485+
QgsGeometry interpolate( double distance );
486486

487487
/** Returns a geometry representing the points shared by this geometry and other. */
488-
QgsGeometry* intersection( const QgsGeometry* geometry ) const /Factory/;
488+
QgsGeometry intersection( const QgsGeometry& geometry ) const;
489489

490490
/** Returns a geometry representing all the points in this geometry and other (a
491491
* union geometry operation).
492492
* @note this operation is not called union since its a reserved word in C++.
493493
*/
494-
QgsGeometry* combine( const QgsGeometry* geometry ) const /Factory/;
494+
QgsGeometry combine( const QgsGeometry& geometry ) const;
495495

496496
/** Merges any connected lines in a LineString/MultiLineString geometry and
497497
* converts them to single line strings.
@@ -503,10 +503,10 @@ class QgsGeometry
503503
QgsGeometry mergeLines() const;
504504

505505
/** Returns a geometry representing the points making up this geometry that do not make up other. */
506-
QgsGeometry* difference( const QgsGeometry* geometry ) const /Factory/;
506+
QgsGeometry difference( const QgsGeometry& geometry ) const;
507507

508508
/** Returns a geometry representing the points making up this geometry that do not make up other. */
509-
QgsGeometry* symDifference( const QgsGeometry* geometry ) const /Factory/;
509+
QgsGeometry symDifference( const QgsGeometry& geometry ) const;
510510

511511
/** Returns an extruded version of this geometry. */
512512
QgsGeometry extrude( double x, double y );
@@ -531,7 +531,7 @@ class QgsGeometry
531531
* @return the converted geometry or nullptr if the conversion fails.
532532
* @note added in 2.2
533533
*/
534-
QgsGeometry* convertToType( Qgis::GeometryType destType, bool destMultipart = false ) const /Factory/;
534+
QgsGeometry convertToType( Qgis::GeometryType destType, bool destMultipart = false ) const;
535535

536536
/* Accessor functions for getting geometry data */
537537

@@ -564,7 +564,7 @@ class QgsGeometry
564564

565565
/** Return contents of the geometry as a list of geometries
566566
@note added in version 1.1 */
567-
QList<QgsGeometry*> asGeometryCollection() const /Factory/;
567+
QList<QgsGeometry> asGeometryCollection() const;
568568

569569
/** Return contents of the geometry as a QPointF if wkbType is WKBPoint,
570570
* otherwise returns a null QPointF.
@@ -643,7 +643,7 @@ class QgsGeometry
643643
* @param geometryList a list of QgsGeometry* as input
644644
* @returns the new computed QgsGeometry, or null
645645
*/
646-
static QgsGeometry *unaryUnion( const QList<QgsGeometry*>& geometryList ) /Factory/;
646+
static QgsGeometry unaryUnion( const QList<QgsGeometry>& geometryList );
647647

648648
/** Converts the geometry to straight line segments, if it is a curved geometry type.
649649
* @note added in QGIS 2.10
@@ -704,15 +704,15 @@ class QgsGeometry
704704
* @param point source QPointF
705705
* @note added in QGIS 2.7
706706
*/
707-
static QgsGeometry* fromQPointF( QPointF point ) /Factory/;
707+
static QgsGeometry fromQPointF( QPointF point );
708708

709709
/** Construct geometry from a QPolygonF. If the polygon is closed than
710710
* the resultant geometry will be a polygon, if it is open than the
711711
* geometry will be a polyline.
712712
* @param polygon source QPolygonF
713713
* @note added in QGIS 2.7
714714
*/
715-
static QgsGeometry* fromQPolygonF( const QPolygonF& polygon ) /Factory/;
715+
static QgsGeometry fromQPolygonF( const QPolygonF& polygon );
716716

717717
/** Creates a QgsPolyline from a QPolygonF.
718718
* @param polygon source polygon
@@ -768,7 +768,7 @@ class QgsGeometry
768768
* of the geometry for each iteration. Smaller values result in "tighter" smoothing.
769769
* @note added in 2.9
770770
*/
771-
QgsGeometry* smooth( const unsigned int iterations, const double offset ) const;
771+
QgsGeometry smooth( const unsigned int iterations, const double offset ) const;
772772

773773
/** Smooths a polygon using the Chaikin algorithm*/
774774
QgsPolygon smoothPolygon( const QgsPolygon &polygon, const unsigned int iterations = 1, const double offset = 0.25 ) const;
@@ -794,5 +794,7 @@ class QgsGeometry
794794
//! Allows direct construction of QVariants from geometry.
795795
operator QVariant() const;
796796

797+
operator bool() const;
798+
797799
}; // class QgsGeometry
798800

python/core/qgsgeometrysimplifier.sip

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class QgsAbstractGeometrySimplifier
1212
virtual ~QgsAbstractGeometrySimplifier();
1313

1414
//! Returns a simplified version the specified geometry
15-
virtual QgsGeometry* simplify( QgsGeometry* geometry ) const = 0;
15+
virtual QgsGeometry simplify( const QgsGeometry& geometry ) const = 0;
1616
//! Simplifies the specified geometry
1717
virtual bool simplifyGeometry( QgsGeometry* geometry ) const = 0;
1818

@@ -41,7 +41,7 @@ class QgsTopologyPreservingSimplifier : QgsAbstractGeometrySimplifier
4141
virtual ~QgsTopologyPreservingSimplifier();
4242

4343
//! Returns a simplified version the specified geometry
44-
virtual QgsGeometry* simplify( QgsGeometry* geometry ) const;
44+
virtual QgsGeometry simplify( const QgsGeometry& geometry ) const;
4545
//! Simplifies the specified geometry
4646
virtual bool simplifyGeometry( QgsGeometry* geometry ) const;
4747
};

python/core/qgsmaptopixelgeometrysimplifier.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class QgsMapToPixelSimplifier : QgsAbstractGeometrySimplifier
4444
void setSimplifyAlgorithm( SimplifyAlgorithm simplifyAlgorithm );
4545

4646
//! Returns a simplified version the specified geometry
47-
virtual QgsGeometry* simplify( QgsGeometry* geometry ) const;
47+
virtual QgsGeometry simplify( const QgsGeometry& geometry ) const;
4848
//! Simplifies the specified geometry
4949
virtual bool simplifyGeometry( QgsGeometry* geometry ) const;
5050

python/core/qgsogcutils.sip

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class QgsOgcUtils
1313
in default namespace (\verbatim {<Point>...</Point> \endverbatim) or in
1414
"gml" namespace (\verbatim <gml:Point>...</gml:Point> \endverbatim)
1515
*/
16-
static QgsGeometry* geometryFromGML( const QString& xmlString ) /Factory/;
16+
static QgsGeometry geometryFromGML( const QString& xmlString );
1717

1818
/** Static method that creates geometry from GML
1919
*/
20-
static QgsGeometry* geometryFromGML( const QDomNode& geometryNode ) /Factory/;
20+
static QgsGeometry geometryFromGML( const QDomNode& geometryNode );
2121

2222
/** Read rectangle from GML2 Box */
2323
static QgsRectangle rectangleFromGMLBox( const QDomNode& boxNode );

python/core/qgspallabeling.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ class QgsPalLayerSettings
742742

743743
QgsPoint ptZero;
744744
QgsPoint ptOne;
745-
QgsGeometry* extentGeom;
745+
QgsGeometry extentGeom;
746746
int mFeaturesToLabel; // total features that will probably be labeled, may be less (figured before PAL)
747747
int mFeatsSendingToPal; // total features tested for sending into PAL (relative to maxNumLabels)
748748
int mFeatsRegPal; // number of features registered in PAL, when using limitNumLabels

python/gui/qgsrubberband.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class QgsRubberBand: QgsMapCanvasItem
221221
* Returns the rubberband as a Geometry.
222222
* @return A geometry object which reflects the current state of the rubberband.
223223
*/
224-
QgsGeometry* asGeometry();
224+
QgsGeometry asGeometry() const;
225225

226226
virtual void updatePosition();
227227

src/analysis/interpolation/DualEdgeTriangulation.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -3138,9 +3138,7 @@ bool DualEdgeTriangulation::saveAsShapefile( const QString& fileName ) const
31383138
QgsPolyline lineGeom;
31393139
lineGeom.push_back( QgsPoint( p1->getX(), p1->getY() ) );
31403140
lineGeom.push_back( QgsPoint( p2->getX(), p2->getY() ) );
3141-
QgsGeometry* geom = QgsGeometry::fromPolyline( lineGeom );
3142-
edgeLineFeature.setGeometry( *geom );
3143-
delete geom;
3141+
edgeLineFeature.setGeometry( QgsGeometry::fromPolyline( lineGeom ) );
31443142
edgeLineFeature.initAttributes( 1 );
31453143

31463144
//attributes

src/analysis/openstreetmap/qgsosmdatabase.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ void QgsOSMDatabase::exportSpatiaLiteNodes( const QString& tableName, const QStr
410410
if ( skipNull )
411411
continue;
412412

413-
QgsGeometry* geom = QgsGeometry::fromPoint( n.point() );
413+
QgsGeometry geom = QgsGeometry::fromPoint( n.point() );
414414
int col = 0;
415415
sqlite3_bind_int64( stmtInsert, ++col, n.id() );
416416

@@ -423,19 +423,17 @@ void QgsOSMDatabase::exportSpatiaLiteNodes( const QString& tableName, const QStr
423423
sqlite3_bind_null( stmtInsert, ++col );
424424
}
425425

426-
sqlite3_bind_blob( stmtInsert, ++col, geom->asWkb(), ( int ) geom->wkbSize(), SQLITE_STATIC );
426+
sqlite3_bind_blob( stmtInsert, ++col, geom.asWkb(), ( int ) geom.wkbSize(), SQLITE_STATIC );
427427

428428
int insertRes = sqlite3_step( stmtInsert );
429429
if ( insertRes != SQLITE_DONE )
430430
{
431431
mError = QString( "Error inserting node %1 [%2]" ).arg( n.id() ).arg( insertRes );
432-
delete geom;
433432
break;
434433
}
435434

436435
sqlite3_reset( stmtInsert );
437436
sqlite3_clear_bindings( stmtInsert );
438-
delete geom;
439437
}
440438

441439
sqlite3_finalize( stmtInsert );
@@ -492,7 +490,7 @@ void QgsOSMDatabase::exportSpatiaLiteWays( bool closed, const QString& tableName
492490
if ( skipNull )
493491
continue;
494492

495-
QgsGeometry* geom = closed ? QgsGeometry::fromPolygon( QgsPolygon() << polyline ) : QgsGeometry::fromPolyline( polyline );
493+
QgsGeometry geom = closed ? QgsGeometry::fromPolygon( QgsPolygon() << polyline ) : QgsGeometry::fromPolyline( polyline );
496494
int col = 0;
497495
sqlite3_bind_int64( stmtInsert, ++col, w.id() );
498496

@@ -505,22 +503,20 @@ void QgsOSMDatabase::exportSpatiaLiteWays( bool closed, const QString& tableName
505503
sqlite3_bind_null( stmtInsert, ++col );
506504
}
507505

508-
if ( geom )
509-
sqlite3_bind_blob( stmtInsert, ++col, geom->asWkb(), ( int ) geom->wkbSize(), SQLITE_STATIC );
506+
if ( !geom.isEmpty() )
507+
sqlite3_bind_blob( stmtInsert, ++col, geom.asWkb(), ( int ) geom.wkbSize(), SQLITE_STATIC );
510508
else
511509
sqlite3_bind_null( stmtInsert, ++col );
512510

513511
int insertRes = sqlite3_step( stmtInsert );
514512
if ( insertRes != SQLITE_DONE )
515513
{
516514
mError = QString( "Error inserting way %1 [%2]" ).arg( w.id() ).arg( insertRes );
517-
delete geom;
518515
break;
519516
}
520517

521518
sqlite3_reset( stmtInsert );
522519
sqlite3_clear_bindings( stmtInsert );
523-
delete geom;
524520
}
525521

526522
sqlite3_finalize( stmtInsert );

0 commit comments

Comments
 (0)