Skip to content

Fix static initialization order fiasco for psram_allocator globals#837

Closed
robertlipe wants to merge 5 commits into
PlummersSoftwareLLC:mainfrom
robertlipe:fix/global-static-order
Closed

Fix static initialization order fiasco for psram_allocator globals#837
robertlipe wants to merge 5 commits into
PlummersSoftwareLLC:mainfrom
robertlipe:fix/global-static-order

Conversation

@robertlipe

Copy link
Copy Markdown
Collaborator

Description

Wrap global static objects that use psram_allocator in Meyer's singletons (Construct-On-First-Use) to prevent PSRAM allocation before the memory subsystem is initialized during app_main().

Affected globals:

  • AnimatedGIFs map and g_ptrGIFDecoder (PatternAnimatedGIF.h)
  • weatherIcons map (PatternWeather.h)
  • CWebServer::mySettingSpecs (webserver.cpp/h)

This is particularly terrible because we call psramAlloc() before main() but psram isn't configured until after we're inside of setup().

Contributing requirements

  • I read the contribution guidelines in CONTRIBUTING.md.
  • I understand the BlinkenPerBit metric, and maximized it in this PR.
  • I selected main as the target branch.
  • All code herein is subjected to the license terms in COPYING.txt.

@robertlipe robertlipe marked this pull request as ready for review April 8, 2026 14:47
Comment thread include/effects/matrix/PatternAnimatedGIF.h Outdated
@davepl

davepl commented Apr 9, 2026 via email

Copy link
Copy Markdown
Contributor

@davepl

davepl commented Apr 9, 2026 via email

Copy link
Copy Markdown
Contributor

@robertlipe

robertlipe commented Apr 9, 2026 via email

Copy link
Copy Markdown
Collaborator Author

@davepl

davepl commented Apr 9, 2026 via email

Copy link
Copy Markdown
Contributor

@robertlipe

robertlipe commented Apr 9, 2026 via email

Copy link
Copy Markdown
Collaborator Author

@rbergen

rbergen commented Apr 9, 2026

Copy link
Copy Markdown
Collaborator

@robertlipe @davepl I'll make the comment here because both of you seem to be watching this PR: I'm mildly confused about what the discussion you're having means for this and subsequent PRs. As in, there is a mention of a future PR from Dave, but I don't currently know if that's supposed to be a PR that alters this one, or a follow-up PR, assuming that the one I'm adding this comment to is merged.

The facts being that I now have the time to review things and I like empty PR lists :), I will proceed with reviewing and merging this PR, #801 and #836, and in that order for reasons of review volume - unless I encounter merge conflicts on that path, in which case I will skip the PRs that turn red.

@rbergen

rbergen commented Apr 9, 2026

Copy link
Copy Markdown
Collaborator

Ah, there are pending changes to this PR, which means only the other two I mentioned remain. Onwards and upwards.

@robertlipe

robertlipe commented Apr 9, 2026 via email

Copy link
Copy Markdown
Collaborator Author

@rbergen

rbergen commented Apr 9, 2026

Copy link
Copy Markdown
Collaborator

I can make this draft to show that I have ownership until then (I know you
have mixed feelings about that) if it improves your day in some way.

Actually, I don't like PRs being opened in draft status, but I don't mind them entering that state if the review cycle yields that it should be ignored for a while. I know, that's a very specific Rutgerian distinction, but sorrynotsorry - yeah, I actually did that.

All that being as it may, it doesn't matter anymore, because I have now already established there's nothing to look at. Which makes I will not look at it again until I'm informed (by GitHub or otherwise) that things here have changed.

@rbergen

rbergen commented Apr 26, 2026

Copy link
Copy Markdown
Collaborator

Converting this to draft after all as the influx of new PRs has increased, and then clarity of status matters.

@rbergen rbergen marked this pull request as draft April 26, 2026 07:52
@robertlipe

robertlipe commented Apr 26, 2026 via email

Copy link
Copy Markdown
Collaborator Author

robertlipe added a commit to robertlipe/NightDriverStrip that referenced this pull request May 16, 2026
…ersSoftwareLLC#837 feedback)

- In PatternWeather.h and PatternAnimatedGIF.h, store results of singleton getters in local variables.
- In src/webserver.cpp, use local variable for GetMySettingSpecs() in LoadDeviceSettingSpecs().
- Add AGENTS.md and GEMINI.md for AI agent context.
…ather to use class member data.

This commit moves problematic static/global variables into class instance members, providing a cleaner architecture and resolving Static Initialization Order Fiasco (SIOF) issues without relying on Meyers Singletons.

Key changes:
- CWebServer: Converted static members and methods to instance members/methods; updated route handlers to capture 'this'.
- PatternAnimatedGIF: Moved decoder state and GIF maps to instance data; implemented an 'ActiveInstance' tracker for decoder callbacks.
- PatternWeather: Migrated 'weatherIcons' to an instance member.
- Verified build and syntax via 'tab5_demo' environment.
This commit cleans up outdated comments and explicit allocator logic that are no longer needed now that the platform-default allocator policy handles PSRAM routing automatically.

- include/interfaces.h: Removed broken references to non-existent 'make_unique_internal' helpers.
- src/jsonserializer.cpp: Removed explicit 'JsonPsramAllocator' in favor of default allocation.
- src/main.cpp, include/effects/strip/fireeffect.h, src/gfxbase.cpp: Updated comments to remove references to legacy project-side PSRAM helpers.
This commit adds a pre-build script 'tools/pio_audit.py' that executes 'audit_globals_order.py' and 'audit_include_rules.py'. These audits are now a mandatory gate in the build process; any violation will terminate the build with an error, ensuring project standards for header inclusion and ordering are maintained.
@robertlipe robertlipe force-pushed the fix/global-static-order branch 2 times, most recently from ba37153 to 234331d Compare May 17, 2026 10:11
…udits.

This commit resolves numerous header inclusion and ordering issues to satisfy the new project-wide build gate:
- Added missing 'globals.h' to 'iservice.h', 'crgbw.h', 'pixelformat.h'.
- Alphabetized system and local includes in 'ledstripeffect.h', 'gfxbase.h', 'ws281xgfx.h', 'webserver.h', 'crgbw.h', 'pixelformat.h', 'ws281xoutputmanager.cpp', 'deviceconfig.cpp', 'webserver.cpp', 'ledstripeffect.cpp', and 'systemcontainer.cpp'.
- Enforced tiering by moving system headers before local headers where necessary.
@robertlipe robertlipe force-pushed the fix/global-static-order branch from 234331d to 5aa8f68 Compare May 17, 2026 10:13
This commit refactors GFXBase::getPolarMap to replace manual double-checked locking and std::unique_ptr with a thread-safe C++11 magic static. The large PolarMapArray is still allocated on the heap to ensure it lands in PSRAM (lazy initialization during setup), but the code is now more idiomatic and readable. Preserved the detailed comments on geometric calculations.
@robertlipe robertlipe marked this pull request as ready for review May 17, 2026 10:51
@robertlipe

Copy link
Copy Markdown
Collaborator Author

PTAL.

This special little flower needed some time to grow and bloom. It uncovered out all kinds of interesting language issues, like our Noise stuff and our Gfx accessors, and it leapfrogged a couple of other PRs that were rooted in this one, but I'm NOW officially happy (enough) with this.

@rbergen

rbergen commented May 17, 2026

Copy link
Copy Markdown
Collaborator

Thank you @robertlipe. Not to cause even more clashes between intended changes, I'm going to wait with merging this until issue #877 has been resolved. The reason is that quite a few of the files this PR is touching are also mentioned in an analysis of possible causes for what's described in that issue.

rbergen added a commit that referenced this pull request Jun 6, 2026
Adapt core #837 refactors to current main with safe SIOF fixes, audit integration, and build-flag cleanup
@rbergen

rbergen commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

Superseded by #891.

@rbergen rbergen closed this Jun 6, 2026
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.

3 participants