Skip to content

Commit d3eb164

Browse files
committedJun 28, 2016
[BUGFIX] QgsMSLayerCache: remove layer from QgsMapLayerRegistry before delete it
In QGIS Server, layers can be added to QgsMapLayerRegistry and delete by QgsMSLayerCache. This means that QgsMapLayerRegistry can have reference to deleted pointers.
1 parent 7dcb9cc commit d3eb164

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed
 

‎src/server/qgsmslayercache.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include "qgsmslayercache.h"
1919
#include "qgsmessagelog.h"
20+
#include "qgsmaplayerregistry.h"
21+
#include "qgsmaplayer.h"
2022
#include "qgsvectorlayer.h"
2123
#include "qgslogger.h"
2224
#include <QFile>
@@ -187,6 +189,10 @@ void QgsMSLayerCache::removeLeastUsedEntry()
187189

188190
void QgsMSLayerCache::freeEntryRessources( QgsMSLayerCacheEntry& entry )
189191
{
192+
// remove layer from QgsMapLayerRegistry before delete it
193+
if ( QgsMapLayerRegistry::instance()->mapLayer( entry.layerPointer->id() ) )
194+
QgsMapLayerRegistry::instance()->removeMapLayer( entry.layerPointer->id() );
195+
190196
delete entry.layerPointer;
191197

192198
//remove the temporary files of a layer

2 commit comments

Comments
 (2)

m-kuhn commented on Jun 28, 2016

@m-kuhn
Member

Another approach would be to connect to QgsMapLayer::destroyed when adding a layer to QgsMapLayerRegistry. That would avoid ever ending up in that situation regardless of the context.
Not sure if there's also a drawback to that.

rldhont commented on Jun 29, 2016

@rldhont
ContributorAuthor

I have tested to remove references on QgsMapLayer::destroyed but it doesn't work and segfault QGIS.

Please sign in to comment.