diff --git a/src/_C018.ino b/src/_C018.ino index c3b944d6a..93f763c05 100644 --- a/src/_C018.ino +++ b/src/_C018.ino @@ -77,13 +77,25 @@ struct C018_data_struct { _baudrate = baudrate; // FIXME TD-er: Make force SW serial a proper setting. + if (C018_easySerial != nullptr) { + delete C018_easySerial; + } + C018_easySerial = new (std::nothrow) ESPeasySerial(static_cast(port), serial_rx, serial_tx, false, 64); if (C018_easySerial != nullptr) { - myLora = new rn2xx3(*C018_easySerial); - myLora->setAsyncMode(true); - myLora->setLastUsedJoinMode(joinIsOTAA); - triggerAutobaud(); + if (myLora != nullptr) { + delete myLora; + } + myLora = new (std::nothrow) rn2xx3(*C018_easySerial); + if (myLora == nullptr) { + delete C018_easySerial; + C018_easySerial = nullptr; + } else { + myLora->setAsyncMode(true); + myLora->setLastUsedJoinMode(joinIsOTAA); + triggerAutobaud(); + } } return isInitialized(); } diff --git a/src/_P016_IR.ino b/src/_P016_IR.ino index c9ff19c87..f2229bc8d 100644 --- a/src/_P016_IR.ino +++ b/src/_P016_IR.ino @@ -226,7 +226,7 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string) addLog(LOG_LEVEL_INFO, F("INIT: IR RX")); addLog(LOG_LEVEL_INFO, F("IR lib Version: " _IRREMOTEESP8266_VERSION_)); } - irReceiver = new IRrecv(irPin, kCaptureBufferSize, P016_TIMEOUT, true); + irReceiver = new (std::nothrow) IRrecv(irPin, kCaptureBufferSize, P016_TIMEOUT, true); # ifdef PLUGIN_016_DEBUG addLog(LOG_LEVEL_INFO, F("P016_PLUGIN_INIT IR receiver created")); # endif // PLUGIN_016_DEBUG diff --git a/src/_P035_IRTX.ino b/src/_P035_IRTX.ino index bc6425632..69b094e9d 100644 --- a/src/_P035_IRTX.ino +++ b/src/_P035_IRTX.ino @@ -99,21 +99,23 @@ boolean Plugin_035(uint8_t function, struct EventStruct *event, String &command) case PLUGIN_INIT: { int irPin = CONFIG_PIN1; - if (Plugin_035_irSender == 0 && validGpio(irPin)) + if (Plugin_035_irSender == nullptr && validGpio(irPin)) { if (loglevelActiveFor(LOG_LEVEL_INFO)) { addLog(LOG_LEVEL_INFO, F("INIT: IR TX")); addLog(LOG_LEVEL_INFO, F("IR lib Version: " _IRREMOTEESP8266_VERSION_)); addLog(LOG_LEVEL_INFO, String(F("Supported Protocols by IRSEND: ")) + listProtocols()); } - Plugin_035_irSender = new IRsend(irPin); - Plugin_035_irSender->begin(); // Start the sender + Plugin_035_irSender = new (std::nothrow) IRsend(irPin); + if (Plugin_035_irSender != nullptr) { + Plugin_035_irSender->begin(); // Start the sender + } } - if (Plugin_035_irSender != 0 && irPin == -1) + if (Plugin_035_irSender != nullptr && irPin == -1) { addLog(LOG_LEVEL_INFO, F("INIT: IR TX Removed")); delete Plugin_035_irSender; - Plugin_035_irSender = 0; + Plugin_035_irSender = nullptr; } #ifdef P016_P035_Extended_AC @@ -129,7 +131,7 @@ boolean Plugin_035(uint8_t function, struct EventStruct *event, String &command) { addLog(LOG_LEVEL_INFO, F("INIT AC: IR TX Removed")); delete Plugin_035_commonAc; - Plugin_035_commonAc = 0; + Plugin_035_commonAc = nullptr; } #endif @@ -137,6 +139,22 @@ boolean Plugin_035(uint8_t function, struct EventStruct *event, String &command) break; } + case PLUGIN_EXIT: + { + if (Plugin_035_irSender != nullptr) { + delete Plugin_035_irSender; + Plugin_035_irSender = nullptr; + } + #ifdef P016_P035_Extended_AC + if (Plugin_035_commonAc != nullptr) { + delete Plugin_035_commonAc; + Plugin_035_commonAc = nullptr; + } + #endif + success = true; + break; + } + case PLUGIN_WRITE: { String cmdCode = parseString(command,1); diff --git a/src/_P038_NeoPixel.ino b/src/_P038_NeoPixel.ino index bee1239d1..564d98de7 100644 --- a/src/_P038_NeoPixel.ino +++ b/src/_P038_NeoPixel.ino @@ -31,7 +31,7 @@ #include -Adafruit_NeoPixel *Plugin_038_pixels; +Adafruit_NeoPixel *Plugin_038_pixels = nullptr; #define PLUGIN_038 #define PLUGIN_ID_038 38 @@ -94,23 +94,36 @@ boolean Plugin_038(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_INIT: { - if (!Plugin_038_pixels) + if (Plugin_038_pixels == nullptr) { uint8_t striptype = PCONFIG(1); if (striptype == 1) - Plugin_038_pixels = new Adafruit_NeoPixel(PCONFIG(0), CONFIG_PIN1, NEO_GRB + NEO_KHZ800); + Plugin_038_pixels = new (std::nothrow) Adafruit_NeoPixel(PCONFIG(0), CONFIG_PIN1, NEO_GRB + NEO_KHZ800); else if (striptype == 2) - Plugin_038_pixels = new Adafruit_NeoPixel(PCONFIG(0), CONFIG_PIN1, NEO_GRBW + NEO_KHZ800); + Plugin_038_pixels = new (std::nothrow) Adafruit_NeoPixel(PCONFIG(0), CONFIG_PIN1, NEO_GRBW + NEO_KHZ800); else - Plugin_038_pixels = new Adafruit_NeoPixel(PCONFIG(0), CONFIG_PIN1, NEO_GRB + NEO_KHZ800); + Plugin_038_pixels = new (std::nothrow) Adafruit_NeoPixel(PCONFIG(0), CONFIG_PIN1, NEO_GRB + NEO_KHZ800); - Plugin_038_pixels->begin(); // This initializes the NeoPixel library. + if (Plugin_038_pixels != nullptr) { + Plugin_038_pixels->begin(); // This initializes the NeoPixel library. + } } MaxPixels = PCONFIG(0); + success = Plugin_038_pixels != nullptr; + break; + } + + case PLUGIN_EXIT: + { + if (Plugin_038_pixels != nullptr) { + delete Plugin_038_pixels; + Plugin_038_pixels = nullptr; + } success = true; break; } + case PLUGIN_WRITE: { if (Plugin_038_pixels) diff --git a/src/_P041_NeoClock.ino b/src/_P041_NeoClock.ino index f0cd6f45c..7371a68ac 100644 --- a/src/_P041_NeoClock.ino +++ b/src/_P041_NeoClock.ino @@ -80,15 +80,26 @@ boolean Plugin_041(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_INIT: { - if (!Plugin_041_pixels) + if (Plugin_041_pixels == nullptr) { - Plugin_041_pixels = new Adafruit_NeoPixel(NUM_LEDS, CONFIG_PIN1, NEO_GRB + NEO_KHZ800); - Plugin_041_pixels->begin(); // This initializes the NeoPixel library. + Plugin_041_pixels = new (std::nothrow) Adafruit_NeoPixel(NUM_LEDS, CONFIG_PIN1, NEO_GRB + NEO_KHZ800); + if (Plugin_041_pixels != nullptr) { + Plugin_041_pixels->begin(); // This initializes the NeoPixel library. + } } Plugin_041_red = PCONFIG(0); Plugin_041_green = PCONFIG(1); Plugin_041_blue = PCONFIG(2); - success = true; + success = Plugin_041_pixels != nullptr; + break; + } + + case PLUGIN_EXIT: + { + if (Plugin_041_pixels != nullptr) { + delete Plugin_041_pixels; + Plugin_041_pixels = nullptr; + } break; } diff --git a/src/_P042_Candle.ino b/src/_P042_Candle.ino index 88af364a9..fd4995e94 100644 --- a/src/_P042_Candle.ino +++ b/src/_P042_Candle.ino @@ -273,10 +273,12 @@ boolean Plugin_042(uint8_t function, struct EventStruct *event, String& string) if (Candle_pixels) { delete Candle_pixels; } - Candle_pixels = new Adafruit_NeoPixel(NUM_PIXEL, CONFIG_PIN1, NEO_GRB + NEO_KHZ800); - SetPixelsBlack(); - Candle_pixels->setBrightness(Candle_bright); - Candle_pixels->begin(); + Candle_pixels = new (std::nothrow) Adafruit_NeoPixel(NUM_PIXEL, CONFIG_PIN1, NEO_GRB + NEO_KHZ800); + if (Candle_pixels != nullptr) { + SetPixelsBlack(); + Candle_pixels->setBrightness(Candle_bright); + Candle_pixels->begin(); + } #ifndef BUILD_NO_DEBUG if (loglevelActiveFor(LOG_LEVEL_DEBUG)) { @@ -287,7 +289,16 @@ boolean Plugin_042(uint8_t function, struct EventStruct *event, String& string) #endif } - success = true; + success = Candle_pixels != nullptr; + break; + } + + case PLUGIN_EXIT: + { + if (Candle_pixels != nullptr) { + delete Candle_pixels; + Candle_pixels = nullptr; + } break; } diff --git a/src/_P046_VentusW266.ino b/src/_P046_VentusW266.ino index 93a7e916f..09f196b7d 100644 --- a/src/_P046_VentusW266.ino +++ b/src/_P046_VentusW266.ino @@ -279,7 +279,7 @@ boolean Plugin_046(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_INIT: { if (!P046_data) { - P046_data = new P046_data_struct(); + P046_data = new (std::nothrow) P046_data_struct(); } uint8_t choice = PCONFIG(0); diff --git a/src/_P054_DMX512.ino b/src/_P054_DMX512.ino index 7e41c022d..28956720a 100644 --- a/src/_P054_DMX512.ino +++ b/src/_P054_DMX512.ino @@ -127,10 +127,12 @@ boolean Plugin_054(uint8_t function, struct EventStruct *event, String& string) if (Plugin_054_DMXBuffer) { delete [] Plugin_054_DMXBuffer; } - Plugin_054_DMXBuffer = new uint8_t[Plugin_054_DMXSize]; - memset(Plugin_054_DMXBuffer, 0, Plugin_054_DMXSize); + Plugin_054_DMXBuffer = new (std::nothrow) uint8_t[Plugin_054_DMXSize]; + if (Plugin_054_DMXBuffer != nullptr) { + memset(Plugin_054_DMXBuffer, 0, Plugin_054_DMXSize); + } - success = true; + success = Plugin_054_DMXBuffer != nullptr; break; } diff --git a/src/_P055_Chiming.ino b/src/_P055_Chiming.ino index c471cbf75..d23e84bf1 100644 --- a/src/_P055_Chiming.ino +++ b/src/_P055_Chiming.ino @@ -62,35 +62,20 @@ class CPlugin_055_Data { public: - long millisStateEnd; - long millisChimeTime; - long millisPauseTime; + long millisStateEnd = 0; + long millisChimeTime = 60; + long millisPauseTime = 400; - int pin[4]; - uint8_t lowActive; - uint8_t chimeClock; + int pin[4] = {0}; + uint8_t lowActive = false; + uint8_t chimeClock = true; - char FIFO[PLUGIN_055_FIFO_SIZE]; - uint8_t FIFO_IndexR; - uint8_t FIFO_IndexW; - - void Plugin_055_Data() - { - millisStateEnd = 0; - millisChimeTime = 60; - millisPauseTime = 400; - - for (uint8_t i=0; i<4; i++) - pin[i] = -1; - lowActive = false; - chimeClock = true; - - FIFO_IndexR = 0; - FIFO_IndexW = 0; - } + char FIFO[PLUGIN_055_FIFO_SIZE] = {0}; + uint8_t FIFO_IndexR = 0; + uint8_t FIFO_IndexW = 0; }; -static CPlugin_055_Data* Plugin_055_Data = NULL; +static CPlugin_055_Data* Plugin_055_Data = nullptr; boolean Plugin_055(uint8_t function, struct EventStruct *event, String& string) @@ -188,31 +173,42 @@ boolean Plugin_055(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_INIT: { if (!Plugin_055_Data) - Plugin_055_Data = new CPlugin_055_Data(); + Plugin_055_Data = new (std::nothrow) CPlugin_055_Data(); - Plugin_055_Data->lowActive = Settings.TaskDevicePin1Inversed[event->TaskIndex]; - Plugin_055_Data->millisChimeTime = PCONFIG(0); - Plugin_055_Data->millisPauseTime = PCONFIG(1); - Plugin_055_Data->chimeClock = PCONFIG(2); + if (Plugin_055_Data != nullptr) { + Plugin_055_Data->lowActive = Settings.TaskDevicePin1Inversed[event->TaskIndex]; + Plugin_055_Data->millisChimeTime = PCONFIG(0); + Plugin_055_Data->millisPauseTime = PCONFIG(1); + Plugin_055_Data->chimeClock = PCONFIG(2); - String log = F("Chime: GPIO: "); - for (uint8_t i=0; i<4; i++) - { - int pin = Settings.TaskDevicePin[i][event->TaskIndex]; - Plugin_055_Data->pin[i] = pin; - if (pin >= 0) + String log = F("Chime: GPIO: "); + for (uint8_t i=0; i<4; i++) { - pinMode(pin, OUTPUT); - digitalWrite(pin, Plugin_055_Data->lowActive); + int pin = Settings.TaskDevicePin[i][event->TaskIndex]; + Plugin_055_Data->pin[i] = pin; + if (pin >= 0) + { + pinMode(pin, OUTPUT); + digitalWrite(pin, Plugin_055_Data->lowActive); + } + log += pin; + log += ' '; } - log += pin; - log += ' '; + if (Plugin_055_Data->lowActive) + log += F("!"); + addLog(LOG_LEVEL_INFO, log); + success = true; } - if (Plugin_055_Data->lowActive) - log += F("!"); - addLog(LOG_LEVEL_INFO, log); - success = true; + break; + } + + case PLUGIN_EXIT: + { + if (Plugin_055_Data != nullptr) { + delete Plugin_055_Data; + Plugin_055_Data = nullptr; + } break; } diff --git a/src/_P056_SDS011-Dust.ino b/src/_P056_SDS011-Dust.ino index 987e9a5a3..8d57cfeee 100644 --- a/src/_P056_SDS011-Dust.ino +++ b/src/_P056_SDS011-Dust.ino @@ -22,7 +22,7 @@ #include "ESPEasy-Globals.h" -CjkSDS011 *Plugin_056_SDS = NULL; +CjkSDS011 *Plugin_056_SDS = nullptr; boolean Plugin_056(uint8_t function, struct EventStruct *event, String& string) @@ -105,12 +105,13 @@ boolean Plugin_056(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_INIT: { - if (Plugin_056_SDS) + if (Plugin_056_SDS) { delete Plugin_056_SDS; + } const int16_t serial_rx = CONFIG_PIN1; const int16_t serial_tx = CONFIG_PIN2; const ESPEasySerialPort port = static_cast(CONFIG_PORT); - Plugin_056_SDS = new CjkSDS011(port, serial_rx, serial_tx); + Plugin_056_SDS = new (std::nothrow) CjkSDS011(port, serial_rx, serial_tx); String log = F("SDS : Init OK ESP GPIO-pin RX:"); log += serial_rx; log += F(" TX:"); @@ -124,6 +125,7 @@ boolean Plugin_056(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_EXIT: { // //FIXME: if this plugin is used more than once at the same time, things go horribly wrong :) + // FIXME TD-er: Must implement plugin_data_struct for this // // if (Plugin_056_SDS) // delete Plugin_056_SDS; diff --git a/src/_P062_MPR121_KeyPad.ino b/src/_P062_MPR121_KeyPad.ino index 3c8ad607f..f15943828 100644 --- a/src/_P062_MPR121_KeyPad.ino +++ b/src/_P062_MPR121_KeyPad.ino @@ -210,6 +210,7 @@ boolean Plugin_062(uint8_t function, struct EventStruct *event, String& string) SaveCustomTaskSettings(event->TaskIndex, reinterpret_cast(&(P062_data->StoredSettings)), sizeof(P062_data->StoredSettings)); if (!canCalibrate) { delete P062_data; + P062_data = nullptr; } else { bool clearCalibration = isFormItemChecked(F("p062_clear_calibrate")); if (clearCalibration) { @@ -232,11 +233,12 @@ boolean Plugin_062(uint8_t function, struct EventStruct *event, String& string) P062_data_struct *P062_data = static_cast(getPluginTaskData(event->TaskIndex)); if (nullptr != P062_data) { - success = true; if (!P062_data->init(event->TaskIndex, PCONFIG(0), PCONFIG(1), tbUseCalibration)) { clearPluginTaskData(event->TaskIndex); P062_data = nullptr; } else { + success = true; + uint8_t touch_treshold = PCONFIG(2); if(touch_treshold == 0) { touch_treshold = P062_DEFAULT_TOUCH_TRESHOLD; //default value diff --git a/src/_P065_DRF0299_MP3.ino b/src/_P065_DRF0299_MP3.ino index 480dbacdc..0e80d0dac 100644 --- a/src/_P065_DRF0299_MP3.ino +++ b/src/_P065_DRF0299_MP3.ino @@ -121,6 +121,15 @@ boolean Plugin_065(uint8_t function, struct EventStruct *event, String& string) break; } + case PLUGIN_EXIT: + { + if (P065_easySerial != nullptr) { + delete P065_easySerial; + P065_easySerial = nullptr; + } + break; + } + case PLUGIN_WRITE: { if (!P065_easySerial) { diff --git a/src/_P078_Eastron.ino b/src/_P078_Eastron.ino index 411693609..924dbcdea 100644 --- a/src/_P078_Eastron.ino +++ b/src/_P078_Eastron.ino @@ -217,7 +217,6 @@ boolean Plugin_078(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_INIT: { - Plugin_078_init = true; if (Plugin_078_SoftSerial != NULL) { delete Plugin_078_SoftSerial; Plugin_078_SoftSerial=NULL; @@ -233,9 +232,10 @@ boolean Plugin_078(uint8_t function, struct EventStruct *event, String& string) delete Plugin_078_SDM; Plugin_078_SDM=NULL; } - Plugin_078_SDM = new SDM(*Plugin_078_SoftSerial, baudrate, P078_DEPIN); + Plugin_078_SDM = new (std::nothrow) SDM(*Plugin_078_SoftSerial, baudrate, P078_DEPIN); if (Plugin_078_SDM != nullptr) { Plugin_078_SDM->begin(); + Plugin_078_init = true; success = true; } break; diff --git a/src/_P088_HeatpumpIR.ino b/src/_P088_HeatpumpIR.ino index ef944840c..55585d2b1 100644 --- a/src/_P088_HeatpumpIR.ino +++ b/src/_P088_HeatpumpIR.ino @@ -207,9 +207,10 @@ boolean Plugin_088(uint8_t function, struct EventStruct *event, String& string) { addLog(LOG_LEVEL_INFO, F("P088: Heatpump IR transmitter deactivated")); - if (Plugin_088_irSender != NULL) + if (Plugin_088_irSender != nullptr) { delete Plugin_088_irSender; + Plugin_088_irSender = nullptr; } break; diff --git a/src/_P095_ILI9341.ino b/src/_P095_ILI9341.ino index de35e94ac..65a6c101a 100644 --- a/src/_P095_ILI9341.ino +++ b/src/_P095_ILI9341.ino @@ -261,16 +261,31 @@ boolean Plugin_095(uint8_t function, struct EventStruct *event, String& string) TFT_Settings.address_tft_dc = PIN(1); TFT_Settings.address_tft_rst = PIN(2); TFT_Settings.rotation = PCONFIG(1); + if (tft != nullptr) { + delete tft; + tft = nullptr; + } - tft = new Adafruit_ILI9341(TFT_Settings.address_tft_cs, TFT_Settings.address_tft_dc, TFT_Settings.address_tft_rst); - tft->begin(); - tft->setRotation(TFT_Settings.rotation); - tft->fillScreen(ILI9341_WHITE); - Plugin_095_printText("ESPEasy", 1, 1); - success = true; + tft = new (std::nothrow) Adafruit_ILI9341(TFT_Settings.address_tft_cs, TFT_Settings.address_tft_dc, TFT_Settings.address_tft_rst); + if (tft != nullptr) { + tft->begin(); + tft->setRotation(TFT_Settings.rotation); + tft->fillScreen(ILI9341_WHITE); + Plugin_095_printText("ESPEasy", 1, 1); + success = true; + } break; } + case PLUGIN_EXIT: + { + if (tft != nullptr) { + delete tft; + tft = nullptr; + } + break; + } + case PLUGIN_WRITE: { String tmpString = String(string); diff --git a/src/_P096_eInk.ino b/src/_P096_eInk.ino index dcfc42549..dc7521448 100644 --- a/src/_P096_eInk.ino +++ b/src/_P096_eInk.ino @@ -285,20 +285,36 @@ boolean Plugin_096(uint8_t function, struct EventStruct *event, String& string) EPD_Settings.width = PCONFIG(2); EPD_Settings.height = PCONFIG(3); - eInkScreen = new LOLIN_IL3897(EPD_Settings.width, EPD_Settings.height, EPD_Settings.address_epd_dc, EPD_Settings.address_epd_rst, EPD_Settings.address_epd_cs, EPD_Settings.address_epd_busy); //hardware SPI - plugin_096_sequence_in_progress = false; - eInkScreen->begin(); - eInkScreen->clearBuffer(); + if (eInkScreen != nullptr) { + delete eInkScreen; + eInkScreen = nullptr; + } - eInkScreen->setTextColor(EPD_BLACK); - eInkScreen->setTextSize(3); - eInkScreen->println("ESP Easy"); - eInkScreen->setTextSize(2); - eInkScreen->println("eInk shield"); - eInkScreen->display(); - delay(100); - - success = true; + eInkScreen = new (std::nothrow) LOLIN_IL3897(EPD_Settings.width, EPD_Settings.height, EPD_Settings.address_epd_dc, EPD_Settings.address_epd_rst, EPD_Settings.address_epd_cs, EPD_Settings.address_epd_busy); //hardware SPI + if (eInkScreen != nullptr) { + plugin_096_sequence_in_progress = false; + eInkScreen->begin(); + eInkScreen->clearBuffer(); + + eInkScreen->setTextColor(EPD_BLACK); + eInkScreen->setTextSize(3); + eInkScreen->println("ESP Easy"); + eInkScreen->setTextSize(2); + eInkScreen->println("eInk shield"); + eInkScreen->display(); + delay(100); + + success = true; + } + break; + } + + case PLUGIN_EXIT: + { + if (eInkScreen != nullptr) { + delete eInkScreen; + eInkScreen = nullptr; + } break; } diff --git a/src/_P102_PZEM004Tv3.ino b/src/_P102_PZEM004Tv3.ino index cf6e9e9a2..9f0864fcb 100644 --- a/src/_P102_PZEM004Tv3.ino +++ b/src/_P102_PZEM004Tv3.ino @@ -232,7 +232,7 @@ boolean Plugin_102(uint8_t function, struct EventStruct *event, String& string) } // Hardware serial is RX on 3 and TX on 1 - P102_PZEM_sensor = new PZEM004Tv30(port, rxPin, txPin); + P102_PZEM_sensor = new (std::nothrow) PZEM004Tv30(port, rxPin, txPin); // Sequence for changing PZEM address if (P102_PZEM_ADDR_SET == 1) // if address programming confirmed diff --git a/src/_P109_ThermOLED.ino b/src/_P109_ThermOLED.ino index 7cbd812dc..b466a1091 100644 --- a/src/_P109_ThermOLED.ino +++ b/src/_P109_ThermOLED.ino @@ -279,9 +279,9 @@ boolean Plugin_109(byte function, struct EventStruct *event, String& string) uint8_t OLED_address = Settings.TaskDevicePluginConfig[event->TaskIndex][0]; if (Settings.TaskDevicePluginConfig[event->TaskIndex][2] == 1) { - P109_display = new SSD1306Wire(OLED_address, Settings.Pin_i2c_sda, Settings.Pin_i2c_scl); + P109_display = new (std::nothrow) SSD1306Wire(OLED_address, Settings.Pin_i2c_sda, Settings.Pin_i2c_scl); } else { - P109_display = new SH1106Wire(OLED_address, Settings.Pin_i2c_sda, Settings.Pin_i2c_scl); + P109_display = new (std::nothrow) SH1106Wire(OLED_address, Settings.Pin_i2c_sda, Settings.Pin_i2c_scl); } P109_display->init(); // call to local override of init function P109_display->displayOn(); diff --git a/src/src/DataStructs/Modbus.cpp b/src/src/DataStructs/Modbus.cpp index 843b28b5e..5155f095b 100644 --- a/src/src/DataStructs/Modbus.cpp +++ b/src/src/DataStructs/Modbus.cpp @@ -8,13 +8,26 @@ Modbus::Modbus() : ModbusClient(nullptr), errcnt(0), timeout(0), TXRXstate(MODBUS_IDLE), RXavailable(0), payLoad(0) {} +Modbus::~Modbus() { + if (ModbusClient) { + ModbusClient->flush(); + ModbusClient->stop(); + delete (ModbusClient); + delay(1); + ModbusClient = nullptr; + } +} + bool Modbus::begin(uint8_t function, uint8_t ModbusID, uint16_t ModbusRegister, MODBUS_registerTypes_t type, char *IPaddress) { currentRegister = ModbusRegister; currentFunction = function; incomingValue = type; resultReceived = false; - ModbusClient = new WiFiClient(); + ModbusClient = new (std::nothrow) WiFiClient(); + if (ModbusClient == nullptr) { + return false; + } ModbusClient->setNoDelay(true); ModbusClient->setTimeout(CONTROLLER_CLIENTTIMEOUT_DFLT); timeout = millis(); diff --git a/src/src/DataStructs/Modbus.h b/src/src/DataStructs/Modbus.h index a8f97104a..316525d5d 100644 --- a/src/src/DataStructs/Modbus.h +++ b/src/src/DataStructs/Modbus.h @@ -14,6 +14,7 @@ class Modbus { public: Modbus(void); + ~Modbus(); bool handle(); bool begin(uint8_t function, uint8_t ModbusID, diff --git a/src/src/Helpers/Networking.cpp b/src/src/Helpers/Networking.cpp index 747413d96..854a0d07d 100644 --- a/src/src/Helpers/Networking.cpp +++ b/src/src/Helpers/Networking.cpp @@ -627,13 +627,17 @@ static const IPAddress SSDP_MULTICAST_ADDR(239, 255, 255, 250); bool SSDP_begin() { _pending = false; - if (_server) { + if (_server != nullptr) { _server->unref(); + // FIXME TD-er: Shouldn't this also call delete _server ? - _server = 0; + _server = nullptr; } - _server = new UdpContext; + _server = new (std::nothrow) UdpContext; + if (_server == nullptr) { + return false; + } _server->ref(); ip_addr_t ifaddr; diff --git a/src/src/PluginStructs/P020_data_struct.cpp b/src/src/PluginStructs/P020_data_struct.cpp index 4f19f0f7e..c0fe27e33 100644 --- a/src/src/PluginStructs/P020_data_struct.cpp +++ b/src/src/PluginStructs/P020_data_struct.cpp @@ -20,6 +20,7 @@ P020_Task::P020_Task(taskIndex_t taskIndex) : _taskIndex(taskIndex) { P020_Task::~P020_Task() { stopServer(); + serialEnd(); } bool P020_Task::serverActive(WiFiServer *server) { diff --git a/src/src/PluginStructs/P044_data_struct.cpp b/src/src/PluginStructs/P044_data_struct.cpp index ed1cd37a8..785a7ff21 100644 --- a/src/src/PluginStructs/P044_data_struct.cpp +++ b/src/src/PluginStructs/P044_data_struct.cpp @@ -19,6 +19,7 @@ P044_Task::P044_Task() { P044_Task::~P044_Task() { stopServer(); + serialEnd(); } bool P044_Task::serverActive(WiFiServer *server) { diff --git a/src/src/PluginStructs/P062_data_struct.cpp b/src/src/PluginStructs/P062_data_struct.cpp index 21fd3671f..924c79bbf 100644 --- a/src/src/PluginStructs/P062_data_struct.cpp +++ b/src/src/PluginStructs/P062_data_struct.cpp @@ -14,6 +14,13 @@ P062_data_struct::P062_data_struct() { clearCalibrationData(); // Reset } +P062_data_struct::~P062_data_struct() { + if (keypad != nullptr) { + delete keypad; + keypad = nullptr; + } +} + bool P062_data_struct::init(taskIndex_t taskIndex, uint8_t i2c_addr, bool scancode, @@ -26,7 +33,7 @@ bool P062_data_struct::init(taskIndex_t taskIndex, _keepCalibrationData = keepCalibrationData; if (!keypad) { - keypad = new Adafruit_MPR121(); + keypad = new (std::nothrow) Adafruit_MPR121(); } if (keypad) { keypad->begin(_i2c_addr); diff --git a/src/src/PluginStructs/P062_data_struct.h b/src/src/PluginStructs/P062_data_struct.h index 7fc3c2798..92a9acca5 100644 --- a/src/src/PluginStructs/P062_data_struct.h +++ b/src/src/PluginStructs/P062_data_struct.h @@ -14,6 +14,7 @@ struct P062_data_struct : public PluginTaskData_base { public: P062_data_struct(); + ~P062_data_struct(); bool init(taskIndex_t taskIndex, uint8_t i2c_addr, bool scancode, diff --git a/src/src/PluginStructs/P082_data_struct.cpp b/src/src/PluginStructs/P082_data_struct.cpp index 95a7fd487..29e7275f8 100644 --- a/src/src/PluginStructs/P082_data_struct.cpp +++ b/src/src/PluginStructs/P082_data_struct.cpp @@ -51,7 +51,7 @@ bool P082_data_struct::init(ESPEasySerialPort port, const int16_t serial_rx, con return false; } reset(); - gps = new (std::nothrow) TinyGPSPlus(); + gps = new (std::nothrow) TinyGPSPlus(); easySerial = new (std::nothrow) ESPeasySerial(port, serial_rx, serial_tx); if (easySerial != nullptr) { diff --git a/src/src/PluginStructs/P111_data_struct.cpp b/src/src/PluginStructs/P111_data_struct.cpp index 913f4a77f..7ff358b5f 100644 --- a/src/src/PluginStructs/P111_data_struct.cpp +++ b/src/src/PluginStructs/P111_data_struct.cpp @@ -11,9 +11,20 @@ P111_data_struct::P111_data_struct(uint8_t csPin, uint8_t rstPin) : mfrc522(nullptr), _csPin(csPin), _rstPin(rstPin) {} +P111_data_struct::~P111_data_struct() { + if (mfrc522 != nullptr) { + delete mfrc522; + mfrc522 = nullptr; + } +} + void P111_data_struct::init() { - if (mfrc522 == nullptr){ - mfrc522 = new MFRC522 (_csPin, _rstPin); // Instantiate a MFRC522 + if (mfrc522 != nullptr) { + delete mfrc522; + mfrc522 = nullptr; + } + mfrc522 = new (std::nothrow) MFRC522(_csPin, _rstPin); // Instantiate a MFRC522 + if (mfrc522 != nullptr) { mfrc522->PCD_Init(); // Initialize MFRC522 reader } } diff --git a/src/src/PluginStructs/P111_data_struct.h b/src/src/PluginStructs/P111_data_struct.h index 45f6ed66a..08eff6cbe 100644 --- a/src/src/PluginStructs/P111_data_struct.h +++ b/src/src/PluginStructs/P111_data_struct.h @@ -9,11 +9,12 @@ struct P111_data_struct : public PluginTaskData_base { P111_data_struct(uint8_t csPin, uint8_t rstPin); + ~P111_data_struct(); void init(); uint8_t readCardStatus(unsigned long *key, bool *removedTag); String getCardName(); - MFRC522 *mfrc522; + MFRC522 *mfrc522 = nullptr; uint8_t counter = 0;