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.
This commit is contained in:
Ben Meadors authored and GitHub committed 2026-08-12 18:26:27 -05:00
1 parent a41ddec1a7
commit d27d0c4a7f
1 file changed
+10 -10
+10 -10
View File
@@ -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()