Skip to content

Update pixel buffer after changing matrix dimensions#5675

Open
DedeHai wants to merge 1 commit into
wled:mainfrom
DedeHai:fix_2D_setup_crash
Open

Update pixel buffer after changing matrix dimensions#5675
DedeHai wants to merge 1 commit into
wled:mainfrom
DedeHai:fix_2D_setup_crash

Conversation

@DedeHai

@DedeHai DedeHai commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Fix for #5669

Summary by CodeRabbit

  • Bug Fixes
    • Improved pixel buffer allocation during initialization and LED configuration updates to ensure proper synchronization when LED maps are loaded or modified.

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The PR refactors _pixels buffer allocation into a dedicated updatePixelBuffer() method called during startup and LED map deserialization. This ensures the frame buffer size remains synchronized with LED count changes from matrix initialization and dynamic LED mapping.

Changes

Pixel Buffer Synchronization

Layer / File(s) Summary
Buffer allocation refactoring
wled00/FX_fcn.cpp, wled00/FX.h
updatePixelBuffer() method consolidates buffer reallocation logic, computing required size once. finalizeInit() calls it after matrix/strip setup, and the call is wired into the FX.h startup sequence immediately after finalizeInit().
LED map deserialization buffer sync
wled00/FX_fcn.cpp
deserializeMap() tracks prior getLengthTotal() and conditionally calls updatePixelBuffer() both when matrix setup without a ledmap file changes the length, and after LED map processing/allocation completes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • wled/WLED#4783: Both PRs refactor WS2812FX::finalizeInit() and _pixels buffer allocation in wled00/FX_fcn.cpp.
  • wled/WLED#5023: Both PRs modify WS2812FX::deserializeMap() logic affecting buffer management during LED map deserialization.
  • wled/WLED#5073: Both PRs address LED map handling and buffer synchronization to prevent crashes from inconsistent mapping-derived lengths.

Suggested labels

optimization

Suggested reviewers

  • willmmiles
  • blazoncek
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: updating the pixel buffer after matrix dimension changes, which is the core objective of the PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
wled00/FX_fcn.cpp

In file included from wled00/FX_fcn.cpp:12:
wled00/wled.h:77:10: fatal error: 'Arduino.h' file not found
77 | #include <Arduino.h>
| ^~~~~~~~~~~
1 error generated.
Error: the following clang command did not run successfully:
/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/bin/clang-18
@/tmp/coderabbit-infer/b60cf6b5b3054179381dbfec73d529d738196e36-f109c58280734613/tmp/clang_command_.tmp.030de8.txt
++Contents of '/tmp/coderabbit-infer/b60cf6b5b3054179381dbfec73d529d738196e36-f109c58280734613/tmp/clang_command_.tmp.030de8.txt':
"-cc1" "-load"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../../facebook-clang-plugins/libtooling/build/FacebookClangPlugin.dylib"
"-add-plugin" "BiniouASTExporter" "-plugin-arg-BiniouASTExporter" "-"
"-plugin-arg-BiniouASTExporter" "PREPEND_CURRENT_DIR=1"
"-plugin-arg-BiniouASTExporter" "MAX_STRING_SIZE=65535" "-cc1" "-triple"
"x86_64-unknown-linux-gnu" "-emit-obj" "-mrelax

... [truncated 1074 characters] ...

ins/clang/install/lib/clang/18/include"
"-internal-isystem" "/usr/local/include" "-internal-isystem"
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
"-internal-externc-isystem" "/usr/include/x86_64-linux-gnu"
"-internal-externc-isystem" "/include" "-internal-externc-isystem"
"/usr/include" "-Wno-ignored-optimization-argument" "-Wno-everything"
"-fdeprecated-macro" "-ferror-limit" "19" "-fgnuc-version=4.2.1"
"-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions"
"-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o"
"/tmp/coderabbit-infer/f109c58280734613/file.o" "-x" "c++"
"wled00/FX_fcn.cpp" "-O0" "-fno-builtin" "-include"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../lib/clang_wrappers/global_defines.h"
"-Wno-everything"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
wled00/FX_fcn.cpp (2)

1285-1287: ⚡ Quick win

Add error handling for buffer allocation failure.

If allocate_buffer() returns nullptr, _pixels will be null and could cause issues before show() catches it. Following the defensive pattern used in Segment::setGeometry() (lines 495-503), check allocation success immediately.

🛡️ Proposed error handling
   _pixels = static_cast<uint32_t*>(allocate_buffer(requiredMem, BFRALLOC_ENFORCE_PSRAM | BFRALLOC_NOBYTEACCESS | BFRALLOC_CLEAR));
+  if (!_pixels) {
+    DEBUGFX_PRINTLN(F("!!! Not enough RAM for pixel buffer !!!"));
+    errorFlag = ERR_NORAM_PX;
+  }
   DEBUG_PRINTF_P(PSTR("strip buffer size: %uB\n"), requiredMem);
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@wled00/FX_fcn.cpp` around lines 1285 - 1287, The allocation result from
allocate_buffer(requiredMem, BFRALLOC_ENFORCE_PSRAM | BFRALLOC_NOBYTEACCESS |
BFRALLOC_CLEAR) assigned to _pixels must be checked for nullptr immediately
(similar to Segment::setGeometry()); if it returns nullptr, log an error
including requiredMem and a clear message via DEBUG_PRINTF_P or process logger,
set a safe fallback state (e.g., leave _pixels null and set a flag or
return/error out from the calling init function) to avoid dereferencing before
show() is called, and ensure any subsequent code that assumes a valid buffer
respects that flag or early-returns; update the allocation site where _pixels is
assigned to perform this check and follow the same error-handling pattern used
in Segment::setGeometry.

1281-1281: 💤 Low value

Remove commented dead code.

This commented-out line serves no purpose and should be deleted.

🧹 Proposed cleanup
 void WS2812FX::updatePixelBuffer() {
-  //uint32_t currentMem = 0;
   uint32_t requiredMem = getLengthTotal() * sizeof(uint32_t);

As per coding guidelines: "Remove dead/unused code — justify or delete it" and "Unused or dead code must be justified or removed to keep the codebase clean, maintainable, and readable".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@wled00/FX_fcn.cpp` at line 1281, Remove the dead commented declaration
"//uint32_t currentMem = 0;" from FX_fcn.cpp; delete the commented line
referencing currentMem so the file no longer contains unused commented-out code
(no other changes needed).

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@wled00/FX_fcn.cpp`:
- Around line 1284-1285: The comment is incorrect about enforcing PSRAM when
using BFRALLOC_NOBYTEACCESS; on classic ESP32 (CONFIG_IDF_TARGET_ESP32)
allocate_buffer() will take the heap_caps_malloc_prefer(... MALLOC_CAP_32BIT,
MALLOC_CAP_SPIRAM, MALLOC_CAP_8BIT ...) path when BFRALLOC_NOBYTEACCESS is set,
meaning PSRAM is only a fallback rather than guaranteed; update the inline
comment on the _pixels allocation (near allocate_buffer usage) to state that
32-bit/IRAM is preferred and PSRAM is used only if the preferred 32-bit
allocation isn’t available, or remove BFRALLOC_ENFORCE_PSRAM when also passing
BFRALLOC_NOBYTEACCESS for classic ESP32; reference allocate_buffer,
BFRALLOC_NOBYTEACCESS, BFRALLOC_ENFORCE_PSRAM, heap_caps_malloc_prefer and
p_malloc when making the change.

---

Nitpick comments:
In `@wled00/FX_fcn.cpp`:
- Around line 1285-1287: The allocation result from allocate_buffer(requiredMem,
BFRALLOC_ENFORCE_PSRAM | BFRALLOC_NOBYTEACCESS | BFRALLOC_CLEAR) assigned to
_pixels must be checked for nullptr immediately (similar to
Segment::setGeometry()); if it returns nullptr, log an error including
requiredMem and a clear message via DEBUG_PRINTF_P or process logger, set a safe
fallback state (e.g., leave _pixels null and set a flag or return/error out from
the calling init function) to avoid dereferencing before show() is called, and
ensure any subsequent code that assumes a valid buffer respects that flag or
early-returns; update the allocation site where _pixels is assigned to perform
this check and follow the same error-handling pattern used in
Segment::setGeometry.
- Line 1281: Remove the dead commented declaration "//uint32_t currentMem = 0;"
from FX_fcn.cpp; delete the commented line referencing currentMem so the file no
longer contains unused commented-out code (no other changes needed).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4a92b04a-4f6a-4abc-b8f5-2dcb908101f7

📥 Commits

Reviewing files that changed from the base of the PR and between 30abc01 and b60cf6b.

📒 Files selected for processing (2)
  • wled00/FX.h
  • wled00/FX_fcn.cpp

Comment thread wled00/FX_fcn.cpp
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