Skip to content
Permalink
Browse files

Fix a lot of memory leaks when fetching provider libraries

  • Loading branch information
nyalldawson committed Feb 5, 2017
1 parent 4f7d9cd commit 48e5c09b89d30a1ccfb2336ea9f0bc33f56ba3cf
@@ -1175,7 +1175,7 @@ void QgsZipItem::init()
QgsDebugMsgLevel( "provider " + k, 3 );
// some providers hangs with empty uri (Postgis) etc...
// -> using libraries directly
QLibrary *library = QgsProviderRegistry::instance()->providerLibrary( k );
QScopedPointer< QLibrary > library( QgsProviderRegistry::instance()->providerLibrary( k ) );
if ( library )
{
dataCapabilities_t * dataCapabilities = reinterpret_cast< dataCapabilities_t * >( cast_to_fptr( library->resolve( "dataCapabilities" ) ) );
@@ -62,7 +62,7 @@ QgsDataItemProviderRegistry::QgsDataItemProviderRegistry()

Q_FOREACH ( const QString& key, providersList )
{
QLibrary *library = QgsProviderRegistry::instance()->providerLibrary( key );
QScopedPointer< QLibrary > library( QgsProviderRegistry::instance()->providerLibrary( key ) );
if ( !library )
continue;

@@ -402,7 +402,7 @@ QgsDataProvider *QgsProviderRegistry::provider( QString const & providerKey, QSt

int QgsProviderRegistry::providerCapabilities( const QString &providerKey ) const
{
QLibrary *library = providerLibrary( providerKey );
QScopedPointer< QLibrary > library( providerLibrary( providerKey ) );
if ( !library )
{
return QgsDataProvider::NoDataCapabilities;
@@ -450,19 +450,17 @@ QFunctionPointer QgsProviderRegistry::function( QString const & providerKey,
}
}

QLibrary *QgsProviderRegistry::providerLibrary( QString const & providerKey ) const
QLibrary* QgsProviderRegistry::providerLibrary( QString const & providerKey ) const
{
QLibrary *myLib = new QLibrary( library( providerKey ) );
QScopedPointer< QLibrary > myLib( new QLibrary( library( providerKey ) ) );

QgsDebugMsg( "Library name is " + myLib->fileName() );

if ( myLib->load() )
return myLib;
return myLib.take();

QgsDebugMsg( "Cannot load library: " + myLib->errorString() );

delete myLib;

return nullptr;
}

@@ -94,7 +94,11 @@ class CORE_EXPORT QgsProviderRegistry
QFunctionPointer function( const QString & providerKey,
const QString & functionName );

QLibrary *providerLibrary( const QString & providerKey ) const;
/**
* Returns a new QLibrary for the specified \a providerKey. Ownership of the returned
* object is transferred to the caller and the caller is responsible for deleting it.
*/
QLibrary* providerLibrary( const QString & providerKey ) const;

//! Return list of available providers by their keys
QStringList providerList() const;
@@ -29,8 +29,7 @@ typedef QgsTransaction* createTransaction_t( const QString& connString );

QgsTransaction* QgsTransaction::create( const QString& connString, const QString& providerKey )
{

QLibrary* lib = QgsProviderRegistry::instance()->providerLibrary( providerKey );
QScopedPointer< QLibrary > lib( QgsProviderRegistry::instance()->providerLibrary( providerKey ) );
if ( !lib )
return nullptr;

@@ -40,8 +39,6 @@ QgsTransaction* QgsTransaction::create( const QString& connString, const QString

QgsTransaction* ts = createTransaction( connString );

delete lib;

return ts;
}

@@ -167,7 +164,7 @@ bool QgsTransaction::rollback( QString& errorMsg )

bool QgsTransaction::supportsTransaction( const QgsVectorLayer* layer )
{
QLibrary* lib = QgsProviderRegistry::instance()->providerLibrary( layer->providerType() );
QScopedPointer< QLibrary > lib( QgsProviderRegistry::instance()->providerLibrary( layer->providerType() ) );
if ( !lib )
return false;

@@ -63,7 +63,7 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,

QgsProviderRegistry * pReg = QgsProviderRegistry::instance();

QLibrary *myLib = pReg->providerLibrary( providerKey );
QScopedPointer< QLibrary > myLib( pReg->providerLibrary( providerKey ) );
if ( !myLib )
{
mError = ErrInvalidProvider;
@@ -74,14 +74,11 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
createEmptyLayer_t * pCreateEmpty = reinterpret_cast< createEmptyLayer_t * >( cast_to_fptr( myLib->resolve( "createEmptyLayer" ) ) );
if ( !pCreateEmpty )
{
delete myLib;
mError = ErrProviderUnsupportedFeature;
mErrorMessage = QObject::tr( "Provider %1 has no %2 method" ).arg( providerKey, QStringLiteral( "createEmptyLayer" ) );
return;
}

delete myLib;

// create an empty layer
QString errMsg;
mError = pCreateEmpty( uri, fields, geometryType, crs, overwrite, &mOldToNewAttrIdx, &errMsg, options );
@@ -261,7 +261,7 @@ void QgsRasterFormatSaveOptionsWidget::helpOptions()
if ( mProvider == QLatin1String( "gdal" ) && mFormat != QLatin1String( "" ) && ! mPyramids )
{
// get helpCreationOptionsFormat() function ptr for provider
QLibrary *library = QgsProviderRegistry::instance()->providerLibrary( mProvider );
QScopedPointer< QLibrary > library( QgsProviderRegistry::instance()->providerLibrary( mProvider ) );
if ( library )
{
helpCreationOptionsFormat_t * helpCreationOptionsFormat =
@@ -357,7 +357,7 @@ QString QgsRasterFormatSaveOptionsWidget::validateOptions( bool gui, bool report
else
{
// get validateCreationOptionsFormat() function ptr for provider
QLibrary *library = QgsProviderRegistry::instance()->providerLibrary( mProvider );
QScopedPointer< QLibrary > library( QgsProviderRegistry::instance()->providerLibrary( mProvider ) );
if ( library )
{
validateCreationOptionsFormat_t * validateCreationOptionsFormat =
@@ -45,7 +45,7 @@ QVector<QgsDataItem*> QgsOWSConnectionItem::createChildren()
Q_FOREACH ( const QString& key, QStringList() << "wms" << "WFS" << "wcs" )
{
QgsDebugMsg( "Add connection for provider " + key );
QLibrary *library = QgsProviderRegistry::instance()->providerLibrary( key );
QScopedPointer< QLibrary > library( QgsProviderRegistry::instance()->providerLibrary( key ) );
if ( !library )
{
QgsDebugMsg( "Cannot get provider " + key );

0 comments on commit 48e5c09

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