Skip to content
Permalink
Browse files

Ensure standard expressions functions are deleted on exit

(Avoids a lot of LeakSanitiser noise)
  • Loading branch information
nyalldawson committed Aug 25, 2015
1 parent 8ca4b67 commit 24b8405d58f3b05dda1ae5392d2270253c1b8e94
Showing with 54 additions and 3 deletions.
  1. +15 −0 python/core/qgsexpression.sip
  2. +4 −0 src/core/qgsapplication.cpp
  3. +15 −2 src/core/qgsexpression.cpp
  4. +20 −1 src/core/qgsexpression.h
@@ -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 );

@@ -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()
@@ -1767,14 +1767,16 @@ 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 )
{
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;
}
@@ -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 );

0 comments on commit 24b8405

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