Ensure standard expressions functions are deleted on exit

(Avoids a lot of LeakSanitiser noise)
This commit is contained in:
Nyall Dawson 2015-08-25 21:37:57 +10:00
parent 8ca4b6751c
commit 24b8405d58
4 changed files with 54 additions and 3 deletions

View File

@ -299,9 +299,24 @@ class QgsExpression
static const QList<QgsExpression::Function *>& Functions();
static const QStringList& BuiltinFunctions();
/** Registers a function to the expression engine. This is required to allow expressions to utilise the function.
* @param function function to register
* @returns true on successful registration
* @see unregisterFunction
*/
static bool registerFunction( Function* function );
/** Unregisters a function from the expression engine. The function will no longer be usable in expressions.
* @param name function name
* @see registerFunction
*/
static bool unregisterFunction( QString name );
/** Deletes all registered functions whose ownership have been transferred to the expression engine.
* @note added in QGIS 2.12
*/
static void cleanRegisteredFunctions();
// tells whether the identifier is a name of existing function
static bool isFunctionName( const QString& name );

View File

@ -22,6 +22,7 @@
#include "qgsmaplayerregistry.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsproviderregistry.h"
#include "qgsexpression.h"
#include <QDir>
#include <QFile>
@ -634,6 +635,9 @@ void QgsApplication::exitQgis()
//This isn't strictly necessary (since we're exiting anyway) but doing so prevents a lot of
//LeakSanitiser noise which hides real issues
QgsApplication::sendPostedEvents( 0, QEvent::DeferredDelete );
//delete all registered functions from expression engine (see above comment)
QgsExpression::cleanRegisteredFunctions();
}
QString QgsApplication::showSettings()

View File

@ -1767,7 +1767,7 @@ static QVariant fcnGetLayerProperty( const QVariantList& values, const QgsExpres
return QVariant();
}
bool QgsExpression::registerFunction( QgsExpression::Function* function )
bool QgsExpression::registerFunction( QgsExpression::Function* function, bool transferOwnership )
{
int fnIdx = functionIndex( function->name() );
if ( fnIdx != -1 )
@ -1775,6 +1775,8 @@ bool QgsExpression::registerFunction( QgsExpression::Function* function )
return false;
}
QgsExpression::gmFunctions.append( function );
if ( transferOwnership )
QgsExpression::gmOwnedFunctions.append( function );
return true;
}
@ -1794,7 +1796,11 @@ bool QgsExpression::unregisterFunction( QString name )
return false;
}
void QgsExpression::cleanRegisteredFunctions()
{
qDeleteAll( QgsExpression::gmOwnedFunctions );
QgsExpression::gmOwnedFunctions.clear();
}
QStringList QgsExpression::gmBuiltinFunctions;
@ -1842,6 +1848,7 @@ const QStringList& QgsExpression::BuiltinFunctions()
}
QList<QgsExpression::Function*> QgsExpression::gmFunctions;
QList<QgsExpression::Function*> QgsExpression::gmOwnedFunctions;
const QList<QgsExpression::Function*>& QgsExpression::Functions()
{
@ -1978,6 +1985,12 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
;
QgsExpressionContextUtils::registerContextFunctions();
//QgsExpression has ownership of all built-in functions
Q_FOREACH ( QgsExpression::Function* func, gmFunctions )
{
gmOwnedFunctions << func;
}
}
return gmFunctions;
}

View File

@ -490,9 +490,28 @@ class CORE_EXPORT QgsExpression
static QStringList gmBuiltinFunctions;
static const QStringList& BuiltinFunctions();
static bool registerFunction( Function* function );
/** Registers a function to the expression engine. This is required to allow expressions to utilise the function.
* @param function function to register
* @param transferOwnership set to true to transfer ownership of function to expression engine
* @returns true on successful registration
* @see unregisterFunction
*/
static bool registerFunction( Function* function, bool transferOwnership = false );
/** Unregisters a function from the expression engine. The function will no longer be usable in expressions.
* @param name function name
* @see registerFunction
*/
static bool unregisterFunction( QString name );
//! List of functions owned by the expression engine
static QList<Function*> gmOwnedFunctions;
/** Deletes all registered functions whose ownership have been transferred to the expression engine.
* @note added in QGIS 2.12
*/
static void cleanRegisteredFunctions();
// tells whether the identifier is a name of existing function
static bool isFunctionName( const QString& name );