Skip to content

Commit

Permalink
do not use unique_ptr for QGraphicsItem
Browse files Browse the repository at this point in the history
since the ownership of the item is transferred to the scene
this leads to a crash when deleting the object holding the pointer

since the ownership is transferred back when removing the item, one should
take care of resetting the pointer no canvas deletion

this should fix #28962
  • Loading branch information
3nids committed Jun 19, 2019
1 parent 95271e2 commit 2f7ca05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/gui/qgssnapindicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,43 @@
QgsSnapIndicator::QgsSnapIndicator( QgsMapCanvas *canvas )
: mCanvas( canvas )
{
mCanvasDestroyedConnection = QObject::connect( canvas, &QgsMapCanvas::destroyed, [ = ]()
{
mCanvas = nullptr;
mSnappingMarker = nullptr;
} );
}

QgsSnapIndicator::~QgsSnapIndicator() = default;
QgsSnapIndicator::~QgsSnapIndicator()
{
if ( mSnappingMarker && mCanvas )
{
mCanvas->scene()->removeItem( mSnappingMarker );
delete mSnappingMarker;
}

QObject::disconnect( mCanvasDestroyedConnection );
};

void QgsSnapIndicator::setMatch( const QgsPointLocator::Match &match )
{
mMatch = match;

if ( !mMatch.isValid() )
{
mSnappingMarker.reset();
if ( mSnappingMarker )
{
mCanvas->scene()->removeItem( mSnappingMarker );
delete mSnappingMarker; // need to delete since QGraphicsSene::removeItem transfers back ownership
}
mSnappingMarker = nullptr;
QToolTip::hideText();
}
else
{
if ( !mSnappingMarker )
{
mSnappingMarker.reset( new QgsVertexMarker( mCanvas ) );
mSnappingMarker = new QgsVertexMarker( mCanvas ); // ownership of the marker is transferred to QGraphicsScene
mSnappingMarker->setIconSize( QgsGuiUtils::scaleIconSize( 10 ) );
mSnappingMarker->setPenWidth( QgsGuiUtils::scaleIconSize( 3 ) );
}
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgssnapindicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class GUI_EXPORT QgsSnapIndicator

QgsMapCanvas *mCanvas;
QgsPointLocator::Match mMatch;
std::unique_ptr<QgsVertexMarker> mSnappingMarker;
QgsVertexMarker *mSnappingMarker = nullptr;
QMetaObject::Connection mCanvasDestroyedConnection;
};

#endif // QGSSNAPINDICATOR_H

0 comments on commit 2f7ca05

Please sign in to comment.