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.
This commit is contained in:
Mark Liversedge
2016-11-11 19:45:24 +00:00
parent 2fb8124e00
commit 6965d88cf1
4 changed files with 13 additions and 2 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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