From 6965d88cf1fc9fdabf267d3ceec7b157af76efca Mon Sep 17 00:00:00 2001 From: Mark Liversedge Date: Fri, 11 Nov 2016 19:45:24 +0000 Subject: [PATCH] Fix UserMetric calculation error .. add a reference count for clones and originals, noting that for user metrics the factory gets a clone and the original is always deleted at startup. .. additionally when the clone in the factory is deleted on metric changes we finally delete the program that was created initially. .. note there is also a fix to let the configuration settings spot when aggregate zero and istime change in options as these were not spotted before. --- src/Core/Context.cpp | 3 ++- src/Core/DataFilter.h | 2 ++ src/Metrics/UserMetric.cpp | 8 +++++++- src/Metrics/UserMetricSettings.h | 2 ++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Core/Context.cpp b/src/Core/Context.cpp index 0a428d418..fbf98ba05 100644 --- a/src/Core/Context.cpp +++ b/src/Core/Context.cpp @@ -99,6 +99,7 @@ Context::notifyConfigChanged(qint32 state) void Context::userMetricsConfigChanged() { + // read em in... QString metrics = QString("%1/../usermetrics.xml").arg(athlete->home->root().absolutePath()); if (QFile(metrics).exists()) { @@ -124,7 +125,7 @@ Context::userMetricsConfigChanged() // we'll fix it UserMetricSchemaVersion = changed; - // update metric factory + // update metric factory deleting originals RideMetricFactory::instance().removeUserMetrics(); // now add initial metrics -- what about multiple contexts (?) XXX diff --git a/src/Core/DataFilter.h b/src/Core/DataFilter.h index 486f0cc38..4c0e4bb56 100644 --- a/src/Core/DataFilter.h +++ b/src/Core/DataFilter.h @@ -169,6 +169,8 @@ class DataFilter : public QObject static QStringList builtins(); // return list of functions supported + int refcount; // used by user metrics + public slots: QStringList parseFilter(Context *context, QString query, QStringList *list=0); QStringList check(QString query); diff --git a/src/Metrics/UserMetric.cpp b/src/Metrics/UserMetric.cpp index a252a3a25..2bafdd92b 100644 --- a/src/Metrics/UserMetric.cpp +++ b/src/Metrics/UserMetric.cpp @@ -25,6 +25,7 @@ UserMetric::UserMetric(Context *context, UserMetricSettings settings) { // compile the program - built in a context that can close. program = new DataFilter(NULL, context, settings.program); + program->refcount = 1; root = program->root(); rt = &program->rt; @@ -43,6 +44,7 @@ UserMetric::UserMetric(const UserMetric *from) : RideMetric() { this->settings = from->settings; this->program = from->program; + this->program->refcount++; this->root = from->program->root(); this->finit = from->finit; @@ -64,7 +66,11 @@ UserMetric::UserMetric(const UserMetric *from) : RideMetric() UserMetric::~UserMetric() { - if (!clone_ && program) delete program; + // program is shared, only delete when last is destroyed + if (program) { + program->refcount--; + if (!program->refcount) delete program; + } if (clone_) delete rt; } diff --git a/src/Metrics/UserMetricSettings.h b/src/Metrics/UserMetricSettings.h index 30e9e0d90..6f1c190fa 100644 --- a/src/Metrics/UserMetricSettings.h +++ b/src/Metrics/UserMetricSettings.h @@ -57,6 +57,8 @@ class UserMetricSettings { this->conversionSum != right.conversionSum || this->program != right.program || this->precision != right.precision || + this->istime != right.istime || + this->aggzero != right.aggzero || this->fingerprint != right.fingerprint) return true; else