AddressSanitizer: initialization-order-fiasco / crash before main() in a global constructor
A global object used another global from a different file before that one was constructed.
static initialization order fiascocrash before mainAddressSanitizer: initialization-order-fiasco Meaning
The order of dynamic initialisation of globals across translation units is unspecified. A global that uses std::cout, a map or a singleton defined in another .cpp may touch it before it exists, so the crash occurs before main.
Common causes
- Global object depending on a global in another file
- Global
std::string/mapused in a static constructor of a library - Logging used in a global constructor
- Static destructors using destroyed objects at exit
⚡ Quick fix
- Use function-local statics (Meyers singleton) instead of globals
- Make globals
constexpror constant-initialised - Run ASan with
detect_odr_violationandcheck_initialization_order=1
Detailed fix by platform
GCC
ASAN_OPTIONS=check_initialization_order=1:strict_init_order=1 ./app
Code examples
Meyers singleton
// unsafe: global defined in another translation unit
extern std::map<int,int> table;
static int x = table.size(); // table may not exist yet
// safe: construct on first use
std::map<int,int>& table() { static std::map<int,int> t; return t; }How to diagnose
- Before main — Does it crash before main()?
- Globals — Cross-file dependency?
- ASan — Reports initialization-order?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- cannot declare variable to be of abstract type C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions)
- Conditional jump depends on uninitialised value valgrind: Conditional jump or move depends on uninitialised value(s)
Most viewed in C/C++
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- was not declared in this scope C++: error: 'x' was not declared in this scope (undeclared identifier)
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 2 Dec 2026