查找C++静态初始化顺序问题

时间:2022-10-16
本文介绍了查找C++静态初始化顺序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我们在 静态初始化顺序方面遇到了一些问题惨败,我正在寻找方法来梳理大量代码以查找可能出现的情况.关于如何有效地做到这一点有什么建议吗?

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?

关于如何解决静态初始化顺序问题,我得到了一些很好的答案,但这并不是我真正的问题.我想知道如何查找受此问题影响的对象.到目前为止,埃文的回答似乎是最好的;我不认为我们可以使用 valgrind,但我们可能有可以执行类似功能的内存分析工具.这只会在给定构建的初始化顺序错误的情况下捕获问题,并且顺序可以随着每个构建而改变.也许有一个静态分析工具可以解决这个问题.我们的平台是运行在 AIX 上的 IBM XLC/C++ 编译器.

I'm getting some good answers on how to SOLVE the static initialization order problem, but that's not really my question. I'd like to know how to FIND objects that are subject to this problem. Evan's answer seems to be the best so far in this regard; I don't think we can use valgrind, but we may have memory analysis tools that could perform a similar function. That would catch problems only where the initialization order is wrong for a given build, and the order can change with each build. Perhaps there's a static analysis tool that would catch this. Our platform is IBM XLC/C++ compiler running on AIX.

推荐答案

初始化的求解顺序:

首先,这只是一个临时的解决方法,因为您有想要摆脱的全局变量,但还没有时间(您最终会摆脱它们,不是吗?:-)

Solving order of initialization:

First off, this is just a temporary work-around because you have global variables that you are trying to get rid of but just have not had time yet (you are going to get rid of them eventually aren't you? :-)

class A
{
    public:
        // Get the global instance abc
        static A& getInstance_abc()  // return a reference
        {
            static A instance_abc;
            return instance_abc;
        }
};

这将保证它在第一次使用时被初始化并在应用程序终止时被销毁.

This will guarantee that it is initialised on first use and destroyed when the application terminates.

C++11 确实保证这是线程安全的:

C++11 does guarantee that this is thread-safe:

§6.7 [stmt.dcl] p4
如果控件在变量初始化的同时进入声明,则并发执行将等待初始化完成.

§6.7 [stmt.dcl] p4
If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.

但是,C++03官方保证静态函数对象的构造是线程安全的.所以从技术上讲,getInstance_XXX() 方法必须用临界区保护.从好的方面来说,gcc 有一个显式补丁作为编译器的一部分,保证即使存在线程,每个静态函数对象也只会被初始化一次.

However, C++03 does not officially guarantee that the construction of static function objects is thread safe. So technically the getInstance_XXX() method must be guarded with a critical section. On the bright side, gcc has an explicit patch as part of the compiler that guarantees that each static function object will only be initialized once even in the presence of threads.

请注意:不要使用双重检查锁定模式a> 尽量避免锁定的成本.这在 C++03 中不起作用.

Please note: Do not use the double checked locking pattern to try and avoid the cost of the locking. This will not work in C++03.

在创建上,没有问题,因为我们保证先创建后才能使用.

On creation, there are no problems because we guarantee that it is created before it can be used.

在对象被销毁后访问该对象存在潜在问题.只有当您从另一个全局变量的析构函数访问对象时才会发生这种情况(全局,我指的是任何非局部静态变量).

There is a potential problem of accessing the object after it has been destroyed. This only happens if you access the object from the destructor of another global variable (by global, I am referring to any non-local static variable).

解决办法是确保你强制销毁顺序.
请记住,销毁顺序与构造顺序正好相反.所以如果你在你的析构函数中访问对象,你必须保证对象没有被销毁.为此,您必须保证在构造调用对象之前完全构造对象.

The solution is to make sure that you force the order of destruction.
Remember the order of destruction is the exact inverse of the order of construction. So if you access the object in your destructor, you must guarantee that the object has not been destroyed. To do this, you must just guarantee that the object is fully constructed before the calling object is constructed.

class B
{
    public:
        static B& getInstance_Bglob;
        {
            static B instance_Bglob;
            return instance_Bglob;;
        }

        ~B()
        {
             A::getInstance_abc().doSomthing();
             // The object abc is accessed from the destructor.
             // Potential problem.
             // You must guarantee that abc is destroyed after this object.
             // To guarantee this you must make sure it is constructed first.
             // To do this just access the object from the constructor.
        }

        B()
        {
            A::getInstance_abc();
            // abc is now fully constructed.
            // This means it was constructed before this object.
            // This means it will be destroyed after this object.
            // This means it is safe to use from the destructor.
        }
};

这篇关于查找C++静态初始化顺序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:如何使用 GCC 和 ld 删除未使用的 C/C++ 符号? 下一条:如何使用 QueryPerformanceCounter?

相关文章

最新文章