Finding C++ static initialization order problems

后端 未结 12 1001
情话喂你
情话喂你 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:36

    Depending on your compiler, you can place a breakpoint at the constructor initialization code. In Visual C++, this is the _initterm function, which is given a start and end pointer of a list of the functions to call.

    Then step into each function to get the file and function name (assuming you've compiled with debugging info on). Once you have the names, step out of the function (back up to _initterm) and continue until _initterm exits.

    That gives you all the static initializers, not just the ones in your code - it's the easiest way to get an exhaustive list. You can filter out the ones you have no control over (such as those in third-party libraries).

    The theory holds for other compilers but the name of the function and the capability of the debugger may change.

提交回复
热议问题