Skip to content

Commit 8284575

Browse files
committedOct 13, 2017
Even more memory safety in geometry internals by using qgis::make_unique
1 parent 85321b4 commit 8284575

File tree

3 files changed

+60
-70
lines changed

3 files changed

+60
-70
lines changed
 

‎src/core/geometry/qgsgeometry.cpp

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,8 @@ double QgsGeometry::closestSegmentWithContext(
625625

626626
QgsGeometry::OperationResult QgsGeometry::addRing( const QList<QgsPointXY> &ring )
627627
{
628-
QgsLineString *ringLine = new QgsLineString( ring );
629-
return addRing( ringLine );
628+
std::unique_ptr< QgsLineString > ringLine = qgis::make_unique< QgsLineString >( ring );
629+
return addRing( ringLine.release() );
630630
}
631631

632632
QgsGeometry::OperationResult QgsGeometry::addRing( QgsCurve *ring )
@@ -654,11 +654,11 @@ QgsGeometry::OperationResult QgsGeometry::addPart( const QgsPointSequence &point
654654
std::unique_ptr< QgsAbstractGeometry > partGeom;
655655
if ( points.size() == 1 )
656656
{
657-
partGeom.reset( new QgsPoint( points[0] ) );
657+
partGeom = qgis::make_unique< QgsPoint >( points[0] );
658658
}
659659
else if ( points.size() > 1 )
660660
{
661-
std::unique_ptr< QgsLineString > ringLine( new QgsLineString() );
661+
std::unique_ptr< QgsLineString > ringLine = qgis::make_unique< QgsLineString >();
662662
ringLine->setPoints( points );
663663
partGeom = std::move( ringLine );
664664
}
@@ -673,13 +673,13 @@ QgsGeometry::OperationResult QgsGeometry::addPart( QgsAbstractGeometry *part, Qg
673673
switch ( geomType )
674674
{
675675
case QgsWkbTypes::PointGeometry:
676-
detachAndReset( std::unique_ptr< QgsAbstractGeometry >( new QgsMultiPointV2() ) );
676+
detachAndReset( qgis::make_unique< QgsMultiPointV2 >() );
677677
break;
678678
case QgsWkbTypes::LineGeometry:
679-
detachAndReset( std::unique_ptr< QgsAbstractGeometry >( new QgsMultiLineString() ) );
679+
detachAndReset( qgis::make_unique< QgsMultiLineString >() );
680680
break;
681681
case QgsWkbTypes::PolygonGeometry:
682-
detachAndReset( std::unique_ptr< QgsAbstractGeometry >( new QgsMultiPolygonV2() ) );
682+
detachAndReset( qgis::make_unique< QgsMultiPolygonV2 >() );
683683
break;
684684
default:
685685
detachAndReset( nullptr );
@@ -1307,6 +1307,7 @@ QgsPolyline QgsGeometry::asPolyline() const
13071307

13081308
bool doSegmentation = ( QgsWkbTypes::flatType( d->geometry->wkbType() ) == QgsWkbTypes::CompoundCurve
13091309
|| QgsWkbTypes::flatType( d->geometry->wkbType() ) == QgsWkbTypes::CircularString );
1310+
std::unique_ptr< QgsLineString > segmentizedLine;
13101311
QgsLineString *line = nullptr;
13111312
if ( doSegmentation )
13121313
{
@@ -1315,7 +1316,8 @@ QgsPolyline QgsGeometry::asPolyline() const
13151316
{
13161317
return polyLine;
13171318
}
1318-
line = curve->curveToLine();
1319+
segmentizedLine.reset( curve->curveToLine() );
1320+
line = segmentizedLine.get();
13191321
}
13201322
else
13211323
{
@@ -1334,11 +1336,6 @@ QgsPolyline QgsGeometry::asPolyline() const
13341336
polyLine[i].setY( line->yAt( i ) );
13351337
}
13361338

1337-
if ( doSegmentation )
1338-
{
1339-
delete line;
1340-
}
1341-
13421339
return polyLine;
13431340
}
13441341

@@ -1350,14 +1347,16 @@ QgsPolygon QgsGeometry::asPolygon() const
13501347
bool doSegmentation = ( QgsWkbTypes::flatType( d->geometry->wkbType() ) == QgsWkbTypes::CurvePolygon );
13511348

13521349
QgsPolygonV2 *p = nullptr;
1350+
std::unique_ptr< QgsPolygonV2 > segmentized;
13531351
if ( doSegmentation )
13541352
{
13551353
QgsCurvePolygon *curvePoly = qgsgeometry_cast<QgsCurvePolygon *>( d->geometry.get() );
13561354
if ( !curvePoly )
13571355
{
13581356
return QgsPolygon();
13591357
}
1360-
p = curvePoly->toPolygon();
1358+
segmentized.reset( curvePoly->toPolygon() );
1359+
p = segmentized.get();
13611360
}
13621361
else
13631362
{
@@ -1372,10 +1371,6 @@ QgsPolygon QgsGeometry::asPolygon() const
13721371
QgsPolygon polygon;
13731372
convertPolygon( *p, polygon );
13741373

1375-
if ( doSegmentation )
1376-
{
1377-
delete p;
1378-
}
13791374
return polygon;
13801375
}
13811376

@@ -1425,29 +1420,24 @@ QgsMultiPolyline QgsGeometry::asMultiPolyline() const
14251420
QgsMultiPolyline mpl;
14261421
for ( int i = 0; i < nLines; ++i )
14271422
{
1428-
bool deleteLine = false;
14291423
const QgsLineString *line = qgsgeometry_cast<const QgsLineString *>( geomCollection->geometryN( i ) );
1424+
std::unique_ptr< QgsLineString > segmentized;
14301425
if ( !line )
14311426
{
14321427
const QgsCurve *curve = qgsgeometry_cast<const QgsCurve *>( geomCollection->geometryN( i ) );
14331428
if ( !curve )
14341429
{
14351430
continue;
14361431
}
1437-
deleteLine = true;
1438-
line = curve->curveToLine();
1432+
segmentized.reset( curve->curveToLine() );
1433+
line = segmentized.get();
14391434
}
14401435

14411436
QgsPointSequence lineCoords;
14421437
line->points( lineCoords );
14431438
QgsPolyline polyLine;
14441439
convertToPolyline( lineCoords, polyLine );
14451440
mpl.append( polyLine );
1446-
1447-
if ( deleteLine )
1448-
{
1449-
delete line;
1450-
}
14511441
}
14521442
return mpl;
14531443
}
@@ -2564,7 +2554,7 @@ GEOSContextHandle_t QgsGeometry::getGEOSHandler()
25642554

25652555
QgsGeometry QgsGeometry::fromQPointF( QPointF point )
25662556
{
2567-
return QgsGeometry( new QgsPoint( point.x(), point.y() ) );
2557+
return QgsGeometry( qgis::make_unique< QgsPoint >( point.x(), point.y() ) );
25682558
}
25692559

25702560
QgsGeometry QgsGeometry::fromQPolygonF( const QPolygonF &polygon )
@@ -2662,7 +2652,7 @@ QgsGeometry QgsGeometry::smooth( const unsigned int iterations, const double off
26622652
{
26632653
QgsMultiLineString *multiLine = static_cast< QgsMultiLineString * >( d->geometry.get() );
26642654

2665-
std::unique_ptr< QgsMultiLineString > resultMultiline( new QgsMultiLineString() );
2655+
std::unique_ptr< QgsMultiLineString > resultMultiline = qgis::make_unique< QgsMultiLineString> ();
26662656
for ( int i = 0; i < multiLine->numGeometries(); ++i )
26672657
{
26682658
resultMultiline->addGeometry( smoothLine( *( static_cast< QgsLineString * >( multiLine->geometryN( i ) ) ), iterations, offset, minimumDistance, maxAngle ).release() );
@@ -2680,7 +2670,7 @@ QgsGeometry QgsGeometry::smooth( const unsigned int iterations, const double off
26802670
{
26812671
QgsMultiPolygonV2 *multiPoly = static_cast< QgsMultiPolygonV2 * >( d->geometry.get() );
26822672

2683-
std::unique_ptr< QgsMultiPolygonV2 > resultMultiPoly( new QgsMultiPolygonV2() );
2673+
std::unique_ptr< QgsMultiPolygonV2 > resultMultiPoly = qgis::make_unique< QgsMultiPolygonV2 >();
26842674
for ( int i = 0; i < multiPoly->numGeometries(); ++i )
26852675
{
26862676
resultMultiPoly->addGeometry( smoothPolygon( *( static_cast< QgsPolygonV2 * >( multiPoly->geometryN( i ) ) ), iterations, offset, minimumDistance, maxAngle ).release() );
@@ -2705,7 +2695,7 @@ std::unique_ptr< QgsLineString > smoothCurve( const QgsLineString &line, const u
27052695
const double offset, double squareDistThreshold, double maxAngleRads,
27062696
bool isRing )
27072697
{
2708-
std::unique_ptr< QgsLineString > result( new QgsLineString( line ) );
2698+
std::unique_ptr< QgsLineString > result = qgis::make_unique< QgsLineString >( line );
27092699
for ( unsigned int iteration = 0; iteration < iterations; ++iteration )
27102700
{
27112701
QgsPointSequence outputLine;
@@ -2797,7 +2787,7 @@ std::unique_ptr<QgsPolygonV2> QgsGeometry::smoothPolygon( const QgsPolygonV2 &po
27972787
{
27982788
double maxAngleRads = maxAngle * M_PI / 180.0;
27992789
double squareDistThreshold = minimumDistance > 0 ? minimumDistance * minimumDistance : -1;
2800-
std::unique_ptr< QgsPolygonV2 > resultPoly( new QgsPolygonV2 );
2790+
std::unique_ptr< QgsPolygonV2 > resultPoly = qgis::make_unique< QgsPolygonV2 >();
28012791

28022792
resultPoly->setExteriorRing( smoothCurve( *( static_cast< const QgsLineString *>( polygon.exteriorRing() ) ), iterations, offset,
28032793
squareDistThreshold, maxAngleRads, true ).release() );

‎src/core/geometry/qgsgeometryeditutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ QgsGeometry::OperationResult QgsGeometryEditUtils::addPart( QgsAbstractGeometry
125125
std::unique_ptr<QgsCurvePolygon> poly;
126126
if ( QgsWkbTypes::flatType( curve->wkbType() ) == QgsWkbTypes::LineString )
127127
{
128-
poly.reset( new QgsPolygonV2() );
128+
poly = qgis::make_unique< QgsPolygonV2 >();
129129
}
130130
else
131131
{
132-
poly.reset( new QgsCurvePolygon() );
132+
poly = qgis::make_unique< QgsCurvePolygon >();
133133
}
134134
// Ownership is still with part, curve points to the same object and is transferred
135135
// to poly here.

‎src/core/geometry/qgsgeometryfactory.cpp

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -75,51 +75,51 @@ std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkt( const QStr
7575
std::unique_ptr< QgsAbstractGeometry> geom;
7676
if ( trimmed.startsWith( QLatin1String( "Point" ), Qt::CaseInsensitive ) )
7777
{
78-
geom.reset( new QgsPoint() );
78+
geom = qgis::make_unique< QgsPoint >();
7979
}
8080
else if ( trimmed.startsWith( QLatin1String( "LineString" ), Qt::CaseInsensitive ) )
8181
{
82-
geom.reset( new QgsLineString() );
82+
geom = qgis::make_unique< QgsLineString >();
8383
}
8484
else if ( trimmed.startsWith( QLatin1String( "CircularString" ), Qt::CaseInsensitive ) )
8585
{
86-
geom.reset( new QgsCircularString() );
86+
geom = qgis::make_unique< QgsCircularString >();
8787
}
8888
else if ( trimmed.startsWith( QLatin1String( "CompoundCurve" ), Qt::CaseInsensitive ) )
8989
{
90-
geom.reset( new QgsCompoundCurve() );
90+
geom = qgis::make_unique< QgsCompoundCurve>();
9191
}
9292
else if ( trimmed.startsWith( QLatin1String( "Polygon" ), Qt::CaseInsensitive ) )
9393
{
94-
geom.reset( new QgsPolygonV2() );
94+
geom = qgis::make_unique< QgsPolygonV2 >();
9595
}
9696
else if ( trimmed.startsWith( QLatin1String( "CurvePolygon" ), Qt::CaseInsensitive ) )
9797
{
98-
geom.reset( new QgsCurvePolygon() );
98+
geom = qgis::make_unique< QgsCurvePolygon >();
9999
}
100100
else if ( trimmed.startsWith( QLatin1String( "MultiPoint" ), Qt::CaseInsensitive ) )
101101
{
102-
geom.reset( new QgsMultiPointV2() );
102+
geom = qgis::make_unique< QgsMultiPointV2 >();
103103
}
104104
else if ( trimmed.startsWith( QLatin1String( "MultiCurve" ), Qt::CaseInsensitive ) )
105105
{
106-
geom.reset( new QgsMultiCurve() );
106+
geom = qgis::make_unique< QgsMultiCurve >();
107107
}
108108
else if ( trimmed.startsWith( QLatin1String( "MultiLineString" ), Qt::CaseInsensitive ) )
109109
{
110-
geom.reset( new QgsMultiLineString() );
110+
geom = qgis::make_unique< QgsMultiLineString >();
111111
}
112112
else if ( trimmed.startsWith( QLatin1String( "MultiSurface" ), Qt::CaseInsensitive ) )
113113
{
114-
geom.reset( new QgsMultiSurface() );
114+
geom = qgis::make_unique< QgsMultiSurface >();
115115
}
116116
else if ( trimmed.startsWith( QLatin1String( "MultiPolygon" ), Qt::CaseInsensitive ) )
117117
{
118-
geom.reset( new QgsMultiPolygonV2() );
118+
geom = qgis::make_unique< QgsMultiPolygonV2 >();
119119
}
120120
else if ( trimmed.startsWith( QLatin1String( "GeometryCollection" ), Qt::CaseInsensitive ) )
121121
{
122-
geom.reset( new QgsGeometryCollection() );
122+
geom = qgis::make_unique< QgsGeometryCollection >();
123123
}
124124

125125
if ( geom )
@@ -134,12 +134,12 @@ std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkt( const QStr
134134

135135
std::unique_ptr< QgsAbstractGeometry > QgsGeometryFactory::fromPoint( const QgsPointXY &point )
136136
{
137-
return std::unique_ptr< QgsAbstractGeometry >( new QgsPoint( point.x(), point.y() ) );
137+
return qgis::make_unique< QgsPoint >( point.x(), point.y() );
138138
}
139139

140140
std::unique_ptr<QgsMultiPointV2> QgsGeometryFactory::fromMultiPoint( const QgsMultiPoint &multipoint )
141141
{
142-
std::unique_ptr< QgsMultiPointV2 > mp( new QgsMultiPointV2() );
142+
std::unique_ptr< QgsMultiPointV2 > mp = qgis::make_unique< QgsMultiPointV2 >();
143143
QgsMultiPoint::const_iterator ptIt = multipoint.constBegin();
144144
for ( ; ptIt != multipoint.constEnd(); ++ptIt )
145145
{
@@ -156,7 +156,7 @@ std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromPolyline( const Qgs
156156

157157
std::unique_ptr<QgsMultiLineString> QgsGeometryFactory::fromMultiPolyline( const QgsMultiPolyline &multiline )
158158
{
159-
std::unique_ptr< QgsMultiLineString > mLine( new QgsMultiLineString() );
159+
std::unique_ptr< QgsMultiLineString > mLine = qgis::make_unique< QgsMultiLineString >();
160160
for ( int i = 0; i < multiline.size(); ++i )
161161
{
162162
mLine->addGeometry( fromPolyline( multiline.at( i ) ).release() );
@@ -166,7 +166,7 @@ std::unique_ptr<QgsMultiLineString> QgsGeometryFactory::fromMultiPolyline( const
166166

167167
std::unique_ptr<QgsPolygonV2> QgsGeometryFactory::fromPolygon( const QgsPolygon &polygon )
168168
{
169-
std::unique_ptr< QgsPolygonV2 > poly( new QgsPolygonV2() );
169+
std::unique_ptr< QgsPolygonV2 > poly = qgis::make_unique< QgsPolygonV2 >();
170170

171171
QList<QgsCurve *> holes;
172172
for ( int i = 0; i < polygon.size(); ++i )
@@ -189,7 +189,7 @@ std::unique_ptr<QgsPolygonV2> QgsGeometryFactory::fromPolygon( const QgsPolygon
189189

190190
std::unique_ptr< QgsMultiPolygonV2 > QgsGeometryFactory::fromMultiPolygon( const QgsMultiPolygon &multipoly )
191191
{
192-
std::unique_ptr< QgsMultiPolygonV2 > mp( new QgsMultiPolygonV2() );
192+
std::unique_ptr< QgsMultiPolygonV2 > mp = qgis::make_unique< QgsMultiPolygonV2 >();
193193
for ( int i = 0; i < multipoly.size(); ++i )
194194
{
195195
mp->addGeometry( fromPolygon( multipoly.at( i ) ).release() );
@@ -209,7 +209,7 @@ std::unique_ptr<QgsLineString> QgsGeometryFactory::linestringFromPolyline( const
209209
x << it->x();
210210
y << it->y();
211211
}
212-
std::unique_ptr< QgsLineString > line( new QgsLineString( x, y ) );
212+
std::unique_ptr< QgsLineString > line = qgis::make_unique< QgsLineString >( x, y );
213213
return line;
214214
}
215215

@@ -219,31 +219,31 @@ std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkbType( QgsWkb
219219
switch ( type )
220220
{
221221
case QgsWkbTypes::Point:
222-
return std::unique_ptr<QgsAbstractGeometry>( new QgsPoint() );
222+
return qgis::make_unique< QgsPoint >();
223223
case QgsWkbTypes::LineString:
224-
return std::unique_ptr<QgsAbstractGeometry>( new QgsLineString() );
224+
return qgis::make_unique< QgsLineString >();
225225
case QgsWkbTypes::CircularString:
226-
return std::unique_ptr<QgsAbstractGeometry>( new QgsCircularString() );
226+
return qgis::make_unique< QgsCircularString >();
227227
case QgsWkbTypes::CompoundCurve:
228-
return std::unique_ptr<QgsAbstractGeometry>( new QgsCompoundCurve() );
228+
return qgis::make_unique< QgsCompoundCurve >();
229229
case QgsWkbTypes::Polygon:
230-
return std::unique_ptr<QgsAbstractGeometry>( new QgsPolygonV2() );
230+
return qgis::make_unique< QgsPolygonV2 >();
231231
case QgsWkbTypes::CurvePolygon:
232-
return std::unique_ptr<QgsAbstractGeometry>( new QgsCurvePolygon() );
232+
return qgis::make_unique< QgsCurvePolygon >();
233233
case QgsWkbTypes::MultiLineString:
234-
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiLineString() );
234+
return qgis::make_unique< QgsMultiLineString >();
235235
case QgsWkbTypes::MultiPolygon:
236-
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiPolygonV2() );
236+
return qgis::make_unique< QgsMultiPolygonV2 >();
237237
case QgsWkbTypes::MultiPoint:
238-
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiPointV2() );
238+
return qgis::make_unique< QgsMultiPointV2 >();
239239
case QgsWkbTypes::MultiCurve:
240-
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiCurve() );
240+
return qgis::make_unique< QgsMultiCurve >();
241241
case QgsWkbTypes::MultiSurface:
242-
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiSurface() );
242+
return qgis::make_unique< QgsMultiSurface >();
243243
case QgsWkbTypes::GeometryCollection:
244-
return std::unique_ptr<QgsAbstractGeometry>( new QgsGeometryCollection() );
244+
return qgis::make_unique< QgsGeometryCollection >();
245245
case QgsWkbTypes::Triangle:
246-
return std::unique_ptr<QgsAbstractGeometry>( new QgsTriangle() );
246+
return qgis::make_unique< QgsTriangle >();
247247
default:
248248
return nullptr;
249249
}
@@ -256,22 +256,22 @@ std::unique_ptr<QgsGeometryCollection> QgsGeometryFactory::createCollectionOfTyp
256256
switch ( type )
257257
{
258258
case QgsWkbTypes::MultiPoint:
259-
collect.reset( new QgsMultiPointV2() );
259+
collect = qgis::make_unique< QgsMultiPointV2 >();
260260
break;
261261
case QgsWkbTypes::MultiLineString:
262-
collect.reset( new QgsMultiLineString() );
262+
collect = qgis::make_unique< QgsMultiLineString >();
263263
break;
264264
case QgsWkbTypes::MultiCurve:
265-
collect.reset( new QgsMultiCurve() );
265+
collect = qgis::make_unique< QgsMultiCurve >();
266266
break;
267267
case QgsWkbTypes::MultiPolygon:
268-
collect.reset( new QgsMultiPolygonV2() );
268+
collect = qgis::make_unique< QgsMultiPolygonV2 >();
269269
break;
270270
case QgsWkbTypes::MultiSurface:
271-
collect.reset( new QgsMultiSurface() );
271+
collect = qgis::make_unique< QgsMultiSurface >();
272272
break;
273273
case QgsWkbTypes::GeometryCollection:
274-
collect.reset( new QgsGeometryCollection() );
274+
collect = qgis::make_unique< QgsGeometryCollection >();
275275
break;
276276
default:
277277
// should not be possible

0 commit comments

Comments
 (0)