From 1038421544f2809715d7c8da3bab7ab246275e6e Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Mon, 22 Dec 2025 00:31:53 +0100 Subject: [PATCH] atomic pointer swap in allocLeds() --- wled00/FX_fcn.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 84150cff..b4e0894a 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -117,10 +117,19 @@ void Segment::allocLeds() { if ((size > 0) && (!ledsrgb || size > ledsrgbSize)) { //softhack dont allocate zero bytes DEBUG_PRINTF("allocLeds (%d,%d to %d,%d), %u from %u\n", start, startY, stop, stopY, size, ledsrgb?ledsrgbSize:0); - CRGB* oldLedsRgb = ledsrgb; // WLEDMM this makes the re-allocation an anmost-atomic and more threadsafe operation + // WLEDMM this looks a bit over-compilicated, but it makes the re-allocation step an atomic and threadsafe operation + CRGB* oldLedsRgb = ledsrgb; ledsrgb = nullptr; if (oldLedsRgb) free(oldLedsRgb); // we need a bigger buffer, so free the old one first - ledsrgb = (CRGB*)calloc(size, 1); // WLEDMM This is an OS call, so we should not wrap it in portEnterCRITICAL + CRGB* newLedsRgb = (CRGB*)calloc(1, size); // WLEDMM This is an OS call, so we should not wrap it in portEnterCRITICAL + #if defined(ARDUINO_ARCH_ESP32) + portENTER_CRITICAL(&s_wled_strip_mux); + #endif + ledsrgb = newLedsRgb; + #if defined(ARDUINO_ARCH_ESP32) + portEXIT_CRITICAL(&s_wled_strip_mux); + #endif + ledsrgbSize = ledsrgb?size:0; if (ledsrgb == nullptr) { USER_PRINTLN("allocLeds failed!!");