# AddressSanitizer: initialization-order-fiasco / crash before main() in a global constructor

**Code:** `static initialization order` · **Category:** C/C++ · **Seen on:** C/C++

A global object used another global from a different file before that one was constructed.

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/static-initialization-order-fiasco)
