static initialization order C/C++ Origin: Application & API OSI layer 7 · Application Last updated 2 Dec 2026

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.

Exact message you may see static initialization order fiascocrash before mainAddressSanitizer: initialization-order-fiasco
Seen on: C/C++

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/map used in a static constructor of a library
  • Logging used in a global constructor
  • Static destructors using destroyed objects at exit

⚡ Quick fix

  1. Use function-local statics (Meyers singleton) instead of globals
  2. Make globals constexpr or constant-initialised
  3. Run ASan with detect_odr_violation and check_initialization_order=1

Detailed fix by platform

GCC

  1. ASAN_OPTIONS=check_initialization_order=1:strict_init_order=1 ./app

Code examples

Meyers singleton

cpp
// 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

  1. Before main — Does it crash before main()?
  2. Globals — Cross-file dependency?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…cannot declare variable to be of abstract type — C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions) (C/C++)cannot declare variab…Conditional jump depends on uninitialised value — valgrind: Conditional jump or move depends on uninitialised value(s) (C/C++)Conditional jump depe…ODR violation — AddressSanitizer: odr-violation: global 'x' at ... / One Definition Rule (C/C++)ODR violationreference to is ambiguous — C++: error: reference to 'X' is ambiguous (using namespace std conflicts) (C/C++)reference to is ambig…Segmentation fault — C/C++: Segmentation fault (core dumped) — SIGSEGV from a null, dangling or out-of-bounds pointer (C/C++)Segmentation faultuse-of-uninitialized-value — MemorySanitizer: use-of-uninitialized-value (C/C++)use-of-uninitialized-…static in…

Explore the map of this error Full error map →

🧠 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.