Skip to content

Commit

Permalink
[BUGFIX] QgsMSLayerCache: remove layer from QgsMapLayerRegistry befor…
Browse files Browse the repository at this point in the history
…e 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.
  • Loading branch information
rldhont committed Jun 28, 2016
1 parent 7dcb9cc commit d3eb164
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/server/qgsmslayercache.cpp
Expand Up @@ -17,6 +17,8 @@

#include "qgsmslayercache.h"
#include "qgsmessagelog.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaplayer.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include <QFile>
Expand Down Expand Up @@ -187,6 +189,10 @@ void QgsMSLayerCache::removeLeastUsedEntry()

void QgsMSLayerCache::freeEntryRessources( QgsMSLayerCacheEntry& entry )
{
// remove layer from QgsMapLayerRegistry before delete it
if ( QgsMapLayerRegistry::instance()->mapLayer( entry.layerPointer->id() ) )
QgsMapLayerRegistry::instance()->removeMapLayer( entry.layerPointer->id() );

delete entry.layerPointer;

//remove the temporary files of a layer
Expand Down

2 comments on commit d3eb164

@m-kuhn
Copy link
Member

@m-kuhn m-kuhn commented on d3eb164 Jun 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.