Skip to content

ESP32 RMT5: registerTxCallback() leaks a TxCallbackContext every time a channel is recreated #4993

Closed

Description

@DavidSchinazi

Board / MCU

ESP32

LED Chipset

WS2812B / NeoPixel

FastLED Version

3.10.8

Arduino / PlatformIO version

PlatformIO Core 6.2.0.post1 with the pioarduino platform 53.03.10 (Arduino-ESP32 3.1.0, ESP-IDF 5.3.2)

Operating System

macOS

Description

I hit this issue on a project of mine. Claude helped me diagnose it and write the repro below. I've confirmed that the repro works locally on my hardware, and I think what it's saying is pretty credible in this case.

Rmt5PeripheralESPImpl::registerTxCallback() heap-allocates a TxCallbackContext on every call and never frees it.
Its comment assumes this is a one-time allocation per channel, but ChannelEngineRMTImpl calls it every time it
recreates a channel, which can happen on every frame. Each recreation leaks one context, until operator new fails and,
with -fno-exceptions, the device aborts.

I expected memory use to stay flat while sending frames. Instead, free heap drops steadily until the device aborts.

Where

  • The allocation, and the comment that assumes it only happens once per channel:
    // Allocate wrapper context for callback forwarding
    // Note: This is a small, one-time allocation per channel. The context
    // lives for the lifetime of the channel and is not freed (channels are
    // typically created once and reused). For strict correctness, this could
    // be tracked and freed in deleteChannel(), but the overhead is minimal.
    TxCallbackContext* ctx = new TxCallbackContext(); // ok bare allocation
  • deleteChannel() only calls rmt_del_channel(), so nothing frees the context:
    bool Rmt5PeripheralESPImpl::deleteChannel(void* channel_handle) FL_NO_EXCEPT {
    if (channel_handle == nullptr) {
    FL_WARN("Rmt5PeripheralESP: channel_handle is nullptr");
    return false;
    }
    // Delegate to ESP-IDF
    rmt_channel_handle_t channel = static_cast<rmt_channel_handle_t>(channel_handle);
    esp_err_t err = rmt_del_channel(channel);
    if (err != ESP_OK) {
    FL_LOG_RMT("RMT5_PERIPH: Failed to delete channel: " << esp_err_to_name(err));
    return false;
    }
    FL_LOG_RMT("RMT5_PERIPH: Channel deleted successfully");
    return true;
  • Paths that recreate a channel and call registerChannelCallback() → registerTxCallback() again:
    • acquireChannel() "Strategy 2" reuses any idle channel for a different pin. configureChannel() then deletes and
      recreates the channel because the pin changed, and registers the callback again:
      // Strategy 2: Find any idle non-DMA channel (requires reconfiguration)
      #if !FL_RMT_STATIC_ALLOCATION
      for (auto &ch : mChannels) {
      if (!ch.inUse && ch.channel && !ch.useDMA) {
      ch.inUse = true;
      configureChannel(&ch, pin, timing, dataSize);
      if (!ch.channel || !ch.encoder) {
      // Reconfiguration destroyed the old channel and could not
      // build a new one (e.g. invalid pin). Report failure instead
      // of handing out a dead channel.
      ch.inUse = false;
      mAllocationFailed = true;
      return nullptr;
      }
      FL_LOG_RMT("Reconfiguring idle non-DMA channel for pin " << (static_cast<int>(pin)));
      return &ch;
      }
      }

      // If pin changed, destroy and recreate channel
      if (state->channel && state->pin != pin) {
      FL_LOG_RMT("Pin changed from " << (static_cast<int>(state->pin)) << " to " << (static_cast<int>(pin)) << ", recreating channel");

      // Create channel if needed
      if (!state->channel) {
      if (!createChannel(state, pin, timing, dataSize)) {
      FL_LOG_RMT("Failed to recreate channel for pin " << (static_cast<int>(pin)));
      return;
      }
      // Register callback after creation (state pointer is already stable
      // here)
      if (!registerChannelCallback(state)) {
    • reconfigureForNetwork() recreates idle channels and registers their callbacks again whenever the network state
      changes and the memory block counts differ:
      // Delete channel
      if (state.channel) {
      mPeripheral.waitAllDone(state.channel, 100);
      mPeripheral.deleteChannel(state.channel);
      state.channel = nullptr;
      }
      // Free DMA if this channel was using it
      if (state.useDMA) {
      memMgr.rollbackDMA(state.memoryChannelId,
      true); // true = TX channel
      mDMAChannelsInUse--;
      state.useDMA = false;
      }
      // Free memory allocation
      memMgr.rollbackAllocation(state.memoryChannelId,
      true); // true = TX channel
      // Recreate channel with Network-appropriate memory allocation
      // Note: createChannel() will call memMgr.allocateTx() with current Network state
      if (createChannel(&state, state.pin, state.timing, 0)) {
      // Re-register callback for the recreated channel
      if (!registerChannelCallback(&state)) {

How I hit it

My project runs an M5Stack ATOM Matrix (classic ESP32) with 4 WS2812B strips, with Wi-Fi and BLE active, on FastLED
3.10.8 under ESP-IDF 5.5.6 (ESPHome 2026.10). It called CLEDController::showLeds() on each strip's controller in
turn, at ~100 FPS. Each of those calls is its own frame (onBeginFrame() … onEndFrame()), so the driver only ever
transmits one strip at a time. By the time the next strip's frame starts, the previous strip's channel is idle, so
Strategy 2 moves that channel to the next strip's pin. That recreates the channel, and leaks one TxCallbackContext,
for every strip on every frame. The device ran for a while, then aborted with the same backtrace as the repro below.
Sending all strips in a single frame made it go away, because each strip then keeps its own channel, and Strategy 1
reuses it without recreating it.

Suggested fix

Don't allocate the context in registerTxCallback(). For example, keep the TxCallbackContext inside the channel's
state, or pass the ChannelState* straight to rmt_tx_register_event_callbacks() as user_data and dispatch from the
static wrapper. Alternatively, track the context per channel handle and free it in deleteChannel().

Workarounds

  • Send all controllers in a single frame, e.g. with FastLED.show(), instead of calling showLeds() on each controller.
  • Keep FASTLED_RMT_MEM_BLOCKS_NETWORK_MODE equal to FASTLED_RMT_MEM_BLOCKS, so that reconfigureForNetwork()
    doesn't recreate channels when Wi-Fi or Bluetooth turns on.

Steps to reproduce

  1. Create a PlatformIO project with this platformio.ini:

    [env:leak]
    platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.10/platform-espressif32.zip
    board = m5stack-atom
    framework = arduino
    lib_deps = fastled/[email protected]
    upload_speed = 1500000
    monitor_speed = 115200
    monitor_filters = esp32_exception_decoder, time
  2. Put the minimal sketch below in src/main.cpp. No LED strips need to be connected.

  3. Run pio run -t upload -t monitor.

  4. Watch the free heap it prints every second. It drops by about 6 KB per second, from 257,936 bytes to 3,960 bytes in
    42 seconds, and then the board aborts in operator new called from registerTxCallback().

Minimal sketch

#include <FastLED.h>

CRGB a[60];
CRGB b[60];
CLEDController* ca;
CLEDController* cb;

void setup() {
  Serial.begin(115200);
  ca = &FastLED.addLeds<WS2812B, 21, GRB>(a, 60);
  cb = &FastLED.addLeds<WS2812B, 25, GRB>(b, 60);
}

void loop() {
  // Each showLeds() call is its own frame, so one idle RMT channel keeps moving between pins 21 and 25.
  ca->showLeds(64);
  cb->showLeds(64);
  static uint32_t last = 0;
  if (millis() - last > 1000) {
    last = millis();
    Serial.printf("free heap: %u\n", (unsigned)esp_get_free_heap_size());
  }
}

Serial / debug output

free heap: 257936
free heap: 251936
free heap: 245884
...
free heap: 10008
free heap: 3960

abort() was called at PC 0x400ebd0f on core 1

  #0  panic_abort                                           esp_system/panic.c:463
  #1  esp_system_abort                                      esp_system/port/esp_system_chip.c:92
  #2  abort                                                 newlib/abort.c:38
  #3  __cxxabiv1::__terminate(void (*)())                   libsupc++/eh_terminate.cc:48
  #4  std::terminate()                                      libsupc++/eh_terminate.cc:58
  #5  __cxa_allocate_exception                              libsupc++/eh_alloc.cc:412
  #6  operator new(unsigned int)                            libsupc++/new_op.cc:54
  #7  fl::detail::Rmt5PeripheralESPImpl::registerTxCallback rmt5_peripheral_esp.cpp.hpp:375
  #8  fl::ChannelEngineRMTImpl::registerChannelCallback     channel_driver_rmt.cpp.hpp:1640
  #9  fl::ChannelEngineRMTImpl::configureChannel            channel_driver_rmt.cpp.hpp:1092
  #10 fl::ChannelEngineRMTImpl::acquireChannel              channel_driver_rmt.cpp.hpp:1540
  #11 fl::ChannelEngineRMTImpl::processPendingChannels      channel_driver_rmt.cpp.hpp:1160
  #12 fl::ChannelEngineRMTImpl::beginTransmission           channel_driver_rmt.cpp.hpp:548
  #13 fl::ChannelEngineRMTImpl::show                        channel_driver_rmt.cpp.hpp:338
  #14 fl::ChannelManager::onEndFrame                        fl/channels/manager.cpp.hpp:599
  #15 fl::EngineEvents::_onEndFrame                         fl/system/engine_events.cpp.hpp:143
  #16 fl::EngineEvents::onEndFrame                          fl/system/engine_events.cpp.hpp:59
  #17 fl::CLEDController::showLeds(unsigned char)           fl/channels/cled_controller.h:247
  #18 loop()                                                src/main.cpp:16

Checklist

  • I have searched existing issues for duplicates
  • I have tested with the latest FastLED release or git master
  • I can reproduce this with a minimal sketch

Activity

  1. added
    status: triage
    type: bug
    on Nov 1, 2026
  2. SuperDaftBros commented on Nov 2, 2026

    @SuperDaftBros

    +1 for this, had a similar issue with Claude helping to track down the source:

    Environment

    • FastLED 3.10.8
    • ESP32-S3 (N16R8), Arduino-ESP32 3.3.12 / ESP-IDF 5.5.5, PlatformIO (pioarduino platform 55.03.312)
    • Six WS2812B strips, 170 LEDs each, on GPIO 1–6, added with FastLED.addLeds<WS2812B, PIN, GRB>(), FastLED.show() at 50 Hz from loop()

    What happens
    After upgrading from 3.9.0, the board runs normally for a few tens of seconds and then aborts in FastLED.show(). Backtrace from the core dump (decoded with addr2line):

    panic_abort / esp_system_abort / abort
    std::terminate
    __cxa_allocate_exception                  (eh_alloc.cc:412: no memory left even for the exception)
    operator new(unsigned int)
    fl::detail::Rmt5PeripheralESPImpl::registerTxCallback(...)        rmt5_peripheral_esp.cpp.hpp:375
    fl::ChannelEngineRMTImpl::registerChannelCallback(ChannelState*)   channel_driver_rmt.cpp.hpp:1640
    fl::ChannelEngineRMTImpl::configureChannel(...)                    channel_driver_rmt.cpp.hpp:1092
    fl::ChannelEngineRMTImpl::acquireChannel(...)                      channel_driver_rmt.cpp.hpp:1540
    fl::ChannelEngineRMTImpl::processPendingChannels()                 channel_driver_rmt.cpp.hpp:1160
    fl::ChannelEngineRMTImpl::beginTransmission(...)                   channel_driver_rmt.cpp.hpp:548
    fl::ChannelEngineRMTImpl::show()                                   channel_driver_rmt.cpp.hpp:338
    fl::ChannelManager::onEndFrame()                                   manager.cpp.hpp:599
    fl::EngineEvents::_onEndFrame()                                    engine_events.cpp.hpp:143
    

    Cause
    Rmt5PeripheralESPImpl::registerTxCallback() allocates a new TxCallbackContext with new on every call and frees it only if rmt_tx_register_event_callbacks() fails. The comment says this is a one-time allocation because channels are "typically created once and reused". But ChannelEngineRMTImpl::acquireChannel() calls configureChannel() (and so registerChannelCallback() → registerTxCallback()) whenever it hands out a channel: on reuse with a matching pin (unless FL_RMT_STATIC_ALLOCATION), and always when it reconfigures an idle channel for another pin. With more strips than RMT TX channels (the S3 has four), that happens for several strips on every frame, so every show() orphans a few contexts. At 50 Hz the heap is gone in under a minute.

    Workaround
    Stay on 3.9.0, or on the S3 avoid the RMT engine altogether: use the LCD_CAM clockless driver through the channel API (FastLED.setExclusiveDriver<fl::Bus::FLEX_IO, 0>() before FastLED.add(fl::ChannelConfig(...))). That works for us: six strips in parallel, show() 8 ms instead of 36 ms on 3.9.0, and free heap steady.

  3. moved this from Triage to Done in FastLED Trackeron Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: triagetype: bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions