diff --git a/esphome/components/esp32_ble/ble_event.h b/esphome/components/esp32_ble/ble_event.h index f929c4662a..cb70d3e018 100644 --- a/esphome/components/esp32_ble/ble_event.h +++ b/esphome/components/esp32_ble/ble_event.h @@ -203,6 +203,7 @@ class BLEEvent { break; default: + // We only handle 4 GAP event types, others are dropped break; } } diff --git a/esphome/components/esp32_ble/ble_event_pool.h b/esphome/components/esp32_ble/ble_event_pool.h index df92c138c3..d118ccf3ab 100644 --- a/esphome/components/esp32_ble/ble_event_pool.h +++ b/esphome/components/esp32_ble/ble_event_pool.h @@ -42,14 +42,17 @@ template class BLEEventPool { RAMAllocator allocator(RAMAllocator::ALLOC_INTERNAL); event = allocator.allocate(1); - if (event != nullptr) { - // Placement new to construct the object - new (event) BLEEvent(); - this->total_created_++; + if (event == nullptr) { + // Memory allocation failed + return nullptr; } + + // Placement new to construct the object + new (event) BLEEvent(); + this->total_created_++; } - return event; // Will be nullptr if allocation failed + return event; } // Return an event to the pool