[LLVMdev] LLVM and the "static initialization order fiasco"

Morten Ofstad
Wed, 10 Nov 2004 11:10:21 -0000

Hello, I'm getting bitten by the "static initialization order
fiasco"

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12

The static data causing the problem is the static
TypeMap<PointerValType, PointerType> PointerTypes; and other typemaps.
The background is that in our product we are using an overridden new
operator to track memory allocations. This system is intialized at the
beginning of main(), and any 'new' before that will not be tracked. At
the end of main any memory which has leaked is reported and after the
end of main, any calls to 'delete' can only be deleting untracked
memory. This is working well as long as you're careful with static
objects, however the typemaps are intialized before main and the objects
they contain are created in main (tracked) but destroyed after main
together with the typemap. That means they end up being allocated by one
memory system and freed by another which is obviously not a good thing.

If there was some way for me to delete the types that have been
constructed before main exits, that would solve the problem... Most
other libraries have Init() and DeInit() functions instead of relying on
static initialization -- I will make something like this myself as a
temporary fix, but are there any opinions on a more long term solution?

m.