From d27d0c4a7f0aa1ac3778fca721837e328550e5a1 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Wed, 12 Aug 2026 18:26:27 -0500 Subject: [PATCH] fix(detectionsensor): release unsent packets and drop heap scratch buffer (#11449) Both sendDetectionMessage() and sendCurrentStateMessage() allocate a packet from packetPool and then, when the primary channel is the public/default channel, log and return without sending or releasing it. Each refused send permanently leaks one packet from the fixed-size pool; with state_broadcast_secs configured, the heartbeat path repeats this on a timer until the pool is exhausted and the device can no longer allocate packets at all. Release the packet in the refusal branch, matching the pattern used in PositionModule. Also replace the per-message 'new char[40]' scratch buffer with a stack buffer (and sprintf with snprintf), removing the manual delete[] bookkeeping on every exit path. --- src/modules/DetectionSensorModule.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/modules/DetectionSensorModule.cpp b/src/modules/DetectionSensorModule.cpp index 1de1bc184c..927fe7b1a5 100644 --- a/src/modules/DetectionSensorModule.cpp +++ b/src/modules/DetectionSensorModule.cpp @@ -128,11 +128,10 @@ int32_t DetectionSensorModule::runOnce() void DetectionSensorModule::sendDetectionMessage() { LOG_DEBUG("Detected event observed. Send message"); - char *message = new char[40]; - sprintf(message, "%s detected", moduleConfig.detection_sensor.name); + char message[40]; + snprintf(message, sizeof(message), "%s detected", moduleConfig.detection_sensor.name); meshtastic_MeshPacket *p = allocDataPacket(); if (!p) { - delete[] message; return; } p->want_ack = false; @@ -147,18 +146,18 @@ void DetectionSensorModule::sendDetectionMessage() if (!channels.isDefaultChannel(0)) { LOG_INFO("Send message id=%d, dest=%x, msg=%.*s", p->id, p->to, p->decoded.payload.size, p->decoded.payload.bytes); service->sendToMesh(p); - } else + } else { LOG_ERROR("Message not allow on Public channel"); - delete[] message; + packetPool.release(p); + } } void DetectionSensorModule::sendCurrentStateMessage(bool state) { - char *message = new char[40]; - sprintf(message, "%s state: %i", moduleConfig.detection_sensor.name, state); + char message[40]; + snprintf(message, sizeof(message), "%s state: %i", moduleConfig.detection_sensor.name, state); meshtastic_MeshPacket *p = allocDataPacket(); if (!p) { - delete[] message; return; } p->want_ack = false; @@ -168,9 +167,10 @@ void DetectionSensorModule::sendCurrentStateMessage(bool state) if (!channels.isDefaultChannel(0)) { LOG_INFO("Send message id=%d, dest=%x, msg=%.*s", p->id, p->to, p->decoded.payload.size, p->decoded.payload.bytes); service->sendToMesh(p); - } else + } else { LOG_ERROR("Message not allow on Public channel"); - delete[] message; + packetPool.release(p); + } } bool DetectionSensorModule::hasDetectionEvent()