diff --git a/src/mesh/RadioLibInterface.cpp b/src/mesh/RadioLibInterface.cpp index 7ef707e0d..6024d06b6 100644 --- a/src/mesh/RadioLibInterface.cpp +++ b/src/mesh/RadioLibInterface.cpp @@ -46,6 +46,16 @@ RadioLibInterface::RadioLibInterface(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE c #endif } +RadioLibInterface::~RadioLibInterface() +{ + // If the static `instance` pointer still references us, clear it. + // A later successful init() may have replaced `instance` with a newer + // interface — don't clobber that case. + if (instance == this) { + instance = nullptr; + } +} + #ifdef ARCH_ESP32 // ESP32 doesn't use that flag #define YIELD_FROM_ISR(x) portYIELD_FROM_ISR() diff --git a/src/mesh/RadioLibInterface.h b/src/mesh/RadioLibInterface.h index 310ca76bb..2859558ed 100644 --- a/src/mesh/RadioLibInterface.h +++ b/src/mesh/RadioLibInterface.h @@ -136,6 +136,13 @@ class RadioLibInterface : public RadioInterface, protected concurrency::Notified RadioLibInterface(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE busy, PhysicalLayer *iface = NULL); + /** + * Clear the static `instance` pointer if it still points at us, so callers + * that check `RadioLibInterface::instance != nullptr` don't dereference a + * freed object after a failed init() + unique_ptr reset. + */ + virtual ~RadioLibInterface(); + virtual ErrorCode send(meshtastic_MeshPacket *p) override; /**