Skip to content

Delete tx/rx queues in stop() to fix a leak across stop/begin cycles - #25

Open
oe1wkl wants to merge 1 commit into
gmag11:mainfrom
oe1wkl:fix/stop-queue-cleanup
Open

oe1wkl wants to merge 1 commit into
gmag11:mainfrom
oe1wkl:fix/stop-queue-cleanup

Conversation

@oe1wkl

@oe1wkl oe1wkl commented May 22, 2026

Copy link
Copy Markdown

Problem

QuickEspNow::stop() deletes the ESP-NOW tasks and calls esp_now_deinit(), but it never releases the tx_queue and rx_queue created in initComms(). Since begin() → initComms() unconditionally creates fresh queues, every stop()/begin() cycle orphans the previous pair.

Each leaked pair is roughly ESPNOW_QUEUE_SIZE × sizeof(comms_tx_queue_item_t) + ESPNOW_QUEUE_SIZE × sizeof(comms_rx_queue_item_t) of internal RAM. On applications that time-multiplex ESP-NOW with regular WiFi (bring ESP-NOW up and down repeatedly), this steadily exhausts the heap.

Fix

Delete both queues at the end of stop() and null the handles. Ordering is safe: the tasks that read/write these queues are deleted earlier in the same function, so the queues are quiescent by the time they're freed. The handles are also default-initialised to nullptr in the header so the if (tx_queue) guards are safe should stop() ever be called before begin().

Found while debugging a device (Morserino-32) that alternates between a WiFi transceiver mode and ESP-NOW games; combined with the already-merged peer-list self-heal in addPeer, this lets ESP-NOW be cycled repeatedly without leaking or crashing.

Notes

  • Touches only stop() and two member initialisers — no behaviour change for callers that start ESP-NOW once.
  • ESP32 path only (QuickEspNow_esp32.{cpp,h}).

stop() deletes the espnow tasks and deinits ESP-NOW, but never releases
the tx_queue and rx_queue created in initComms(). Because begin() always
creates fresh queues, every stop()/begin() cycle orphans the previous
pair (~ESPNOW_QUEUE_SIZE × the queue item size each), steadily leaking
internal RAM on applications that bring ESP-NOW up and down repeatedly
(e.g. devices that time-multiplex ESP-NOW with regular WiFi).

Delete both queues at the end of stop() and null the handles. The tasks
that read/write them are deleted earlier in the same function, so the
queues are quiescent at this point. Handles are also default-initialised
to nullptr so the guards are safe if stop() is ever called before
begin().
oe1wkl added a commit to oe1wkl/Morserino-32 that referenced this pull request May 22, 2026
The released QuickEspNow 0.8.1 has two problems that bite when ESP-NOW is
brought up and down repeatedly (as the M32 does when alternating between
WiFi Trx and ESP-NOW games):

  - addPeer() calls ESP_ERROR_CHECK(esp_now_get_peer(...)) on a peer that
    its own peer_list still tracks but esp_now_deinit() already removed,
    so the second ESP-NOW session abort()s. This is why entering a game
    after a WiFi session currently forces a memory-clearing reboot.
  - stop() never frees the tx/rx queues it created, leaking a few KB per
    cycle.

The crash is already fixed on upstream main (PR gmag11/QuickESPNow#20,
addPeer self-heals a stale peer) but unreleased. The leak fix is our
PR gmag11/QuickESPNow#25. Pin lib_deps to our fork at the commit that
carries both until upstream publishes a release containing them.

No source/behaviour change here — the rebootIfWifiAlreadyUsed() guards
stay in place for now; removing them is a separate, hardware-validated
follow-up. Builds clean on pocketwroom and heltec_wifi_lora_32_V2.

Co-authored-by: Claude Opus 4.7 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant