Skip to content

Commit

Permalink
Safer memory management in QgsHighlight
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 18, 2021
1 parent 2d83a01 commit 1df71f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
43 changes: 20 additions & 23 deletions src/gui/qgshighlight.cpp
Expand Up @@ -52,9 +52,10 @@

QgsHighlight::QgsHighlight( QgsMapCanvas *mapCanvas, const QgsGeometry &geom, QgsMapLayer *layer )
: QgsMapCanvasItem( mapCanvas )
, mGeometry( geom )
, mLayer( layer )

{
mGeometry = !geom.isNull() ? new QgsGeometry( geom ) : nullptr;
init();
}

Expand All @@ -71,9 +72,9 @@ void QgsHighlight::init()
QgsCoordinateTransform ct = mMapCanvas->mapSettings().layerTransform( mLayer );
if ( ct.isValid() )
{
if ( mGeometry )
if ( !mGeometry.isNull() )
{
mGeometry->transform( ct );
mGeometry.transform( ct );
}
else if ( mFeature.hasGeometry() )
{
Expand All @@ -88,11 +89,7 @@ void QgsHighlight::init()
setColor( QColor( Qt::lightGray ) );
}

QgsHighlight::~QgsHighlight()
{
delete mGeometry;
}

QgsHighlight::~QgsHighlight() = default;

void QgsHighlight::setColor( const QColor &color )
{
Expand Down Expand Up @@ -318,7 +315,7 @@ void QgsHighlight::paint( QPainter *p )
QgsFeature feature = mFeature;

if ( pcLayer )
feature.setGeometry( *mGeometry );
feature.setGeometry( mGeometry );

renderer->startRender( context, feature.fields() );
context.expressionContext().setFeature( feature );
Expand Down Expand Up @@ -353,22 +350,22 @@ void QgsHighlight::paint( QPainter *p )
p->drawImage( 0, 0, image );
}
}
else if ( mGeometry )
else if ( !mGeometry.isNull() )
{
p->setPen( mPen );
p->setBrush( mBrush );

switch ( mGeometry->type() )
switch ( mGeometry.type() )
{
case QgsWkbTypes::PointGeometry:
{
if ( !mGeometry->isMultipart() )
if ( !mGeometry.isMultipart() )
{
paintPoint( p, mGeometry->asPoint() );
paintPoint( p, mGeometry.asPoint() );
}
else
{
QgsMultiPointXY m = mGeometry->asMultiPoint();
QgsMultiPointXY m = mGeometry.asMultiPoint();
for ( int i = 0; i < m.size(); i++ )
{
paintPoint( p, m[i] );
Expand All @@ -379,13 +376,13 @@ void QgsHighlight::paint( QPainter *p )

case QgsWkbTypes::LineGeometry:
{
if ( !mGeometry->isMultipart() )
if ( !mGeometry.isMultipart() )
{
paintLine( p, mGeometry->asPolyline() );
paintLine( p, mGeometry.asPolyline() );
}
else
{
QgsMultiPolylineXY m = mGeometry->asMultiPolyline();
QgsMultiPolylineXY m = mGeometry.asMultiPolyline();

for ( int i = 0; i < m.size(); i++ )
{
Expand All @@ -397,13 +394,13 @@ void QgsHighlight::paint( QPainter *p )

case QgsWkbTypes::PolygonGeometry:
{
if ( !mGeometry->isMultipart() )
if ( !mGeometry.isMultipart() )
{
paintPolygon( p, mGeometry->asPolygon() );
paintPolygon( p, mGeometry.asPolygon() );
}
else
{
QgsMultiPolygonXY m = mGeometry->asMultiPolygon();
QgsMultiPolygonXY m = mGeometry.asMultiPolygon();
for ( int i = 0; i < m.size(); i++ )
{
paintPolygon( p, m[i] );
Expand Down Expand Up @@ -440,9 +437,9 @@ void QgsHighlight::updateRect()

setVisible( true );
}
else if ( mGeometry )
else if ( !mGeometry.isNull() )
{
QgsRectangle r = mGeometry->boundingBox();
QgsRectangle r = mGeometry.boundingBox();

if ( r.isEmpty() )
{
Expand All @@ -454,7 +451,7 @@ void QgsHighlight::updateRect()
}

setRect( r );
setVisible( mGeometry );
setVisible( true );
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgshighlight.h
Expand Up @@ -183,7 +183,7 @@ class GUI_EXPORT QgsHighlight : public QgsMapCanvasItem
QColor mFillColor; // line / stroke fillColor property
QBrush mBrush;
QPen mPen;
QgsGeometry *mGeometry = nullptr;
QgsGeometry mGeometry;
QgsMapLayer *mLayer = nullptr;
QgsFeature mFeature;
double mBuffer = 0; // line / stroke buffer in pixels
Expand Down

0 comments on commit 1df71f6

Please sign in to comment.