Finding C++ static initialization order problems

后端 未结 12 990
情话喂你
情话喂你 2020-11-22 10:10

We\'ve run into some problems with the static initialization order fiasco, and I\'m looking for ways to comb through a whole lot of code to find possible occurrences. Any s

12条回答
  •  半阙折子戏
    2020-11-22 10:20

    We've run into some problems with the static initialization order fiasco, and I'm looking for ways to comb through a whole lot of code to find possible occurrences. Any suggestions on how to do this efficiently?

    It's not a trivial problem but at least it can done following fairly simple steps if you have an easy-to-parse intermediate-format representation of your code.

    1) Find all the globals that have non-trivial constructors and put them in a list.

    2) For each of these non-trivially-constructed objects, generate the entire potential-function-tree called by their constructors.

    3) Walk through the non-trivially-constructor function tree and if the code references any other non-trivially constructed globals (which are quite handily in the list you generated in step one), you have a potential early-static-initialization-order issue.

    4) Repeat steps 2 & 3 until you have exhausted the list generated in step 1.

    Note: you may be able to optimize this by only visiting the potential-function-tree once per object class rather than once per global instance if you have multiple globals of a single class.

提交回复
热议问题