From cb9dba4bb6f1cc369bfa278505298befa1106d19 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 17 Oct 2019 17:48:33 +0300 Subject: [PATCH] sensor/pmsx003: only delete object when it is managed by sensor --- code/espurna/sensors/PMSX003Sensor.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/code/espurna/sensors/PMSX003Sensor.h b/code/espurna/sensors/PMSX003Sensor.h index 2e0526c7..cf235982 100644 --- a/code/espurna/sensors/PMSX003Sensor.h +++ b/code/espurna/sensors/PMSX003Sensor.h @@ -158,14 +158,13 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { // --------------------------------------------------------------------- // Public // --------------------------------------------------------------------- - PMSX003Sensor(): BaseSensor() { _count = pms_specs[_type].slot_count; _sensor_id = SENSOR_PMSX003_ID; } ~PMSX003Sensor() { - if (_serial) delete _serial; + removeSerial(); } void setRX(unsigned char pin_rx) { @@ -216,7 +215,7 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { if (!_dirty) return; if (_soft) { - if (_serial) delete _serial; + if (_serial) removeSerial(); _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 64); static_cast(_serial)->enableIntTx(false); } @@ -358,6 +357,13 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { return _slot_values[index]; } + private: + void removeSerial() { + if (_serial && _soft) { + delete static_cast(_serial); + } + } + protected: bool _soft = true; unsigned int _pin_rx;