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

Morten Ofstad morten at hue.no
Wed Nov 10 03:10:21 PST 2004


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.





More information about the llvm-dev mailing list