Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix crashes on exit due to wkt geometry cache
  • Loading branch information
nyalldawson committed Nov 16, 2021
1 parent bfab3f7 commit eb223fd
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/core/geometry/qgsgeometry.cpp
Expand Up @@ -159,19 +159,17 @@ bool QgsGeometry::isNull() const
return !d->geometry;
}

typedef QCache< QString, QgsGeometry > WktCache;
Q_GLOBAL_STATIC_WITH_ARGS( WktCache, sWktCache, ( 2000 ) ) // store up to 2000 geometries
Q_GLOBAL_STATIC( QMutex, sWktMutex )

QgsGeometry QgsGeometry::fromWkt( const QString &wkt )
{
static QCache< QString, QgsGeometry > sWktCache( 2000 ); // store up to 2000 geometries
static QReadWriteLock sCacheLock;

QgsReadWriteLocker lock( sCacheLock, QgsReadWriteLocker::Read );
if ( const QgsGeometry *cached = sWktCache.object( wkt ) )
QMutexLocker lock( sWktMutex() );
if ( const QgsGeometry *cached = sWktCache()->object( wkt ) )
return *cached;
lock.unlock();

const QgsGeometry result( QgsGeometryFactory::geomFromWkt( wkt ) );
lock.changeMode( QgsReadWriteLocker::Write );
sWktCache.insert( wkt, new QgsGeometry( result ), 1 );
sWktCache()->insert( wkt, new QgsGeometry( result ), 1 );
return result;
}

Expand Down

0 comments on commit eb223fd

Please sign in to comment.