c++11 - Fixed-size array of objects initialization order in C++/MSVC -
i have class definitions in project, resembles this:
class { std::map<type> some_map; }; class b { array_of_a[10]; std::map<type> some_map; }; class c { b array_of_b[15]; std::map<type> some_map; }; class d { c array_of_c[5]; std::map<type> some_map; }; class e { d d; };
it compiles in msvc without errors or warnings, when try run program try create on stack object of class e, sometimes (like 50% of runs) weird "access violation writing" exceptions either std::map, or other stl types mutex use inside class e, being thrown during 1 of above classes constructor invocations.
i didn't create explicit threads in program, code runs regular main(), non-deterministic nature of bug gives me feeling msvc did behind-the-scenes optimization me, , decided kind of weird non-serialized initialization me.
what happening here , doing wrong in code? wrong initialize static-sized arrays of objects contain stl class fields?
the problem there no guarantee order initializers run. known "static initialization order fiasco": https://isocpp.org/wiki/faq/ctors#static-init-order
there's info @ link, short version you'll need structure code such object initialized or constructed when first use it. way can guarantee order initialized order use them in code.
Comments
Post a Comment