mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-10-26 22:10:45 +00:00
Merge pull request #5669 from goodprogrammist/bugfix/P163-failed-init-cleanup
[P163] Fix double-free after failed RadSens initialization
This commit is contained in:
2 files changed
+5
-5
No files matched your search
@@ -25,11 +25,10 @@ P163_data_struct::~P163_data_struct() {
|
||||
* init
|
||||
*****************************************************/
|
||||
bool P163_data_struct::init(struct EventStruct *event) {
|
||||
delete sensor; // In case init() is called again
|
||||
sensor = new (std::nothrow) CG_RadSens(RS_DEFAULT_I2C_ADDRESS);
|
||||
|
||||
if ((nullptr != sensor) && sensor->init()) {
|
||||
initialized = true;
|
||||
|
||||
sensor->setLPmode(_lowPowerMode);
|
||||
sensor->setLedState(_ledState);
|
||||
|
||||
@@ -39,6 +38,7 @@ bool P163_data_struct::init(struct EventStruct *event) {
|
||||
}
|
||||
} else {
|
||||
delete sensor; // Clean up if initialization fails
|
||||
sensor = nullptr;
|
||||
addLog(LOG_LEVEL_ERROR, F("RadSens: Initialization failed!"));
|
||||
}
|
||||
return isInitialized();
|
||||
@@ -59,6 +59,8 @@ bool P163_data_struct::plugin_read(struct EventStruct *event) {
|
||||
bool P163_data_struct::setOutputValues(struct EventStruct *event) {
|
||||
bool result = false;
|
||||
|
||||
if (nullptr == sensor) { return result; }
|
||||
|
||||
const uint32_t count = _readIncrement ? sensor->getNumberOfPulses() : sensor->getNumberOfNewPulses();
|
||||
const uint32_t incCount = sensor->getNumberOfPulses();
|
||||
const float iDynamic = sensor->getRadIntensyDynamic();
|
||||
|
||||
@@ -39,7 +39,7 @@ public:
|
||||
bool plugin_write(struct EventStruct *event,
|
||||
String & string);
|
||||
bool isInitialized() const {
|
||||
return initialized;
|
||||
return nullptr != sensor;
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -57,8 +57,6 @@ private:
|
||||
bool _lowPowerMode = false;
|
||||
bool _ledState = true;
|
||||
bool _changeOnly = false;
|
||||
|
||||
bool initialized = false;
|
||||
};
|
||||
|
||||
#endif // ifdef USES_P163
|
||||
|
||||
Reference in new issue
Block a user