fix(esp_event): free queued legacy cleanup ctx on loop delete

When a loop is deleted while an internal legacy "cleanup" event is still
queued (posted by a deferred self-unregistration from within a handler),
esp_event_loop_delete() drained the queue but only freed the post payload,
leaking the heap copy of the handler context allocated for the legacy path.

Free ctx->handler_ctx for queued legacy cleanup events while draining the
queue, mirroring the cleanup done in esp_event_loop_run().

Add a regression test that leaves a legacy cleanup event queued and asserts
no memory is leaked on loop deletion.
This commit is contained in:
Konstantin Kondrashov
2026-07-21 15:33:47 +03:00
parent e8ffb477a0
commit 82e6c831e7
2 changed files with 31 additions and 1 deletions
+6
View File
@@ -830,6 +830,12 @@ esp_err_t esp_event_loop_delete(esp_event_loop_handle_t event_loop)
// Drop existing posts on the queue
esp_event_post_instance_t post;
while (xQueueReceive(loop->queue, &post, 0) == pdTRUE) {
if (post.base == esp_event_handler_cleanup) {
esp_event_remove_handler_context_t* ctx = (esp_event_remove_handler_context_t*)post.data.ptr;
if (ctx->legacy) {
free(ctx->handler_ctx);
}
}
post_instance_delete(&post);
}