Skip to content
Permalink
Browse files

Ensure master PJ object is always cleaned up when the context is dest…

…royed too

(cherry picked from commit 78ba378)
  • Loading branch information
nyalldawson committed Jan 21, 2020
1 parent c1c6ef2 commit 934b767ae430dd7955a69bdb082e8bed397ad7d1
Showing with 26 additions and 4 deletions.
  1. +4 −4 src/core/qgscoordinatereferencesystem.cpp
  2. +22 −0 src/core/qgscoordinatereferencesystem_p.h
@@ -662,7 +662,7 @@ bool QgsCoordinateReferenceSystem::loadFromDatabase( const QString &db, const QS

{
QgsProjUtils::proj_pj_unique_ptr crs( proj_create_from_database( QgsProjContext::get(), auth.toLatin1(), code.toLatin1(), PJ_CATEGORY_CRS, false, nullptr ) );
d->mPj = QgsProjUtils::crsToSingleCrs( crs.get() );
d->setPj( QgsProjUtils::crsToSingleCrs( crs.get() ) );
}

d->mIsValid = static_cast< bool >( d->mPj );
@@ -1495,7 +1495,7 @@ void QgsCoordinateReferenceSystem::setProjString( const QString &proj4String )
PJ_CONTEXT *ctx = QgsProjContext::get();

{
d->mPj.reset( proj_create( ctx, trimmed.toLatin1().constData() ) );
d->setPj( QgsProjUtils::proj_pj_unique_ptr( proj_create( ctx, trimmed.toLatin1().constData() ) ) );
}

if ( !d->mPj )
@@ -1549,7 +1549,7 @@ bool QgsCoordinateReferenceSystem::setWktString( const QString &wkt, bool allowP
PROJ_STRING_LIST warnings = nullptr;
PROJ_STRING_LIST grammerErrors = nullptr;
{
d->mPj.reset( proj_create_from_wkt( QgsProjContext::get(), wkt.toLatin1().constData(), nullptr, &warnings, &grammerErrors ) );
d->setPj( QgsProjUtils::proj_pj_unique_ptr( proj_create_from_wkt( QgsProjContext::get(), wkt.toLatin1().constData(), nullptr, &warnings, &grammerErrors ) ) );
}

res = static_cast< bool >( d->mPj );
@@ -2453,7 +2453,7 @@ bool QgsCoordinateReferenceSystem::loadFromAuthCode( const QString &auth, const
getOperationAndEllipsoidFromProjString( proj4, operation, ellipsoid );
d->mProjectionAcronym = operation;
d->mEllipsoidAcronym.clear();
d->mPj = std::move( crs );
d->setPj( std::move( crs ) );

const QString dbVals = sAuthIdToQgisSrsIdMap.value( QStringLiteral( "%1:%2" ).arg( auth, code ).toUpper() );
QString srsId;
@@ -153,6 +153,22 @@ class QgsCoordinateReferenceSystemPrivate : public QSharedData
// this is the "master" proj object, to be used as a template for new proj objects created on different threads ONLY.
// Always use threadLocalProjObject() instead of this.
QgsProjUtils::proj_pj_unique_ptr mPj;
PJ_CONTEXT *mPjParentContext = nullptr;

void setPj( QgsProjUtils::proj_pj_unique_ptr obj )
{
if ( mPj )
{
PJ_CONTEXT *tmpContext = proj_context_create();
proj_assign_context( mPj.get(), tmpContext );
mPj.reset();
proj_context_destroy( tmpContext );
}

mPj = std::move( obj );
mPjParentContext = QgsProjContext::get();
}

#else
OGRSpatialReferenceH mCRS;
#endif
@@ -204,6 +220,12 @@ class QgsCoordinateReferenceSystemPrivate : public QSharedData
mProjObjects.erase( it );
}

if ( mPjParentContext == pj_context )
{
mPj.reset();
mPjParentContext = nullptr;
}

return mProjObjects.isEmpty();
}
#endif

0 comments on commit 934b767

Please sign in to comment.
You can’t perform that action at this time.