Commit 38a8c362 authored by Arjen Hiemstra's avatar Arjen Hiemstra

Store SensorProperty as QPointer in AggregateSensor

This prevents a crash where the SensorProperty could disappear without
the AggregateSensor detecting it.
parent 416877ed
......@@ -98,10 +98,20 @@ QVariant AggregateSensor::value() const
}
auto it = m_sensors.constBegin();
while (!it.value() && it != m_sensors.constEnd()) {
it++;
}
if (it == m_sensors.constEnd()) {
return QVariant{};
}
QVariant result = it.value()->value();
it++;
for (; it != m_sensors.constEnd(); it++) {
result = m_aggregateFunction(result, it.value()->value());
if (it.value()) {
result = m_aggregateFunction(result, it.value()->value());
}
}
return result;
}
......@@ -112,7 +122,9 @@ void AggregateSensor::subscribe()
SensorProperty::subscribe();
if (!wasSubscribed && isSubscribed()) {
for (auto sensor : qAsConst(m_sensors)) {
sensor->subscribe();
if (sensor) {
sensor->subscribe();
}
}
}
}
......@@ -123,7 +135,9 @@ void AggregateSensor::unsubscribe()
SensorProperty::unsubscribe();
if (wasSubscribed && !isSubscribed()) {
for (auto sensor : qAsConst(m_sensors)) {
sensor->unsubscribe();
if (sensor) {
sensor->unsubscribe();
}
}
}
}
......@@ -192,6 +206,16 @@ void AggregateSensor::updateSensors()
}
}
}
auto itr = m_sensors.begin();
while (itr != m_sensors.end()) {
if (!itr.value()) {
itr = m_sensors.erase(itr);
} else {
++itr;
}
}
delayedEmitDataChanged();
}
......
......@@ -26,6 +26,7 @@
#include <QRegularExpression>
#include <QVariant>
#include <QVector>
#include <QPointer>
#include "SensorObject.h"
#include "SensorPlugin.h"
......@@ -63,7 +64,7 @@ private:
QRegularExpression m_matchObjects;
QString m_matchProperty;
QHash<QString, SensorProperty *> m_sensors;
QHash<QString, QPointer<SensorProperty>> m_sensors;
bool m_dataChangeQueued = false;
int m_dataCompressionDuration = 100;
SensorContainer *m_subsystem;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment