Hard fault while instantiating global resources and initializing objects in setup()

I have been seeing consistent but irregular hard faults on startup in a fairly small project which is running deviceOS 3.0.0 on a boron with SYSTEM_THREAD enabled and SYSTEM_MODE set to automatic. The faults don’t occur when the project is first flashed, but when the boron is unpowered then powered on again.

After some debugging it’s turned out to be an issue where the project’s initialization and particle’s connection process are interfering with one another, causing bad memory access somewhere in the process. The issue is particularly bad around inserting values into instances of std::map, where the faults essentially don’t occur if calls to insert() are commented out.

Wrapping certain steps of the initialization process in ATOMIC_BLOCK or SINGLE_THREADED_BLOCK mostly deals with the issue, but simply changing system mode to semi-automatic and calling Particle.connect after everything is initialized in setup appears to have completely resolved the problem.

I’m happy that this has worked but I still don’t fully understand what could be causing these faults. Could anyone provide a more detailed explanation of what might be happening?