单例死引用问题 [英] Singleton dead reference problem

查看:138
本文介绍了单例死引用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于singleton的很多。我正在考虑单例之间的死参考问题。在net上的每一个引文中,当一个单例调用其析构函数中的其他单例时,遇到这个问题,并且单例已经被破坏,说Log单例可以从许多其他单例的析构函数中调用。

I was reading around a lot about singleton. I am thinking about the dead reference problem between singletons. In every primer on net , this problem is encountered when one singleton calls other singleton in its destructor, and that singleton is already destroyed, say Log singleton can be called from destructor of many other singletons.

我不能想象在其他情况下(除了引用dtr中的其他单例),死引用将是一个问题。你能给我一个现实世界的例子,这样的问题存在,我如何解决它?

I can't imagine when in other case ( except referencing other singletons in dtr ), the dead reference would be a problem. Can you give me a real world example in which such a problem exists , and how can I solve it ?

事情是,我需要在我们的项目中实现几个单例,它们都彼此沟通,我真的很难选择正确的方式。请不要说不使用单身人士,因为这不是我的决定。

The thing is that I need to implement a couple of singletons in our project, which all communicate with each other, and I am having real hard time to choose the right way. Please do not say not to use a singleton, because that's not my decision.

推荐答案

从这里复制: http://stackoverflow.com/questions/335369/finding-c-static-initialization-order-problems/335746#335746 (没有人会遵循a link sorry)

Copied from here: http://stackoverflow.com/questions/335369/finding-c-static-initialization-order-problems/335746#335746 (Nobody would have followed just a link sorry)

另请参阅这篇文章: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern/1008289#1008289

在销毁对象后存在一个潜在的问题。这只会发生,如果你从另一个全局变量的析构函数(通过全局我访问任何非本地静态变量)访问对象。

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 refering to any non local static variable).

解决方案,你必须确保你强制破坏的顺序。

请记住,破坏的顺序与构造的顺序完全相反。所以如果你访问对象在你的析构函数中,你必须确保对象没有被销毁。要做到这一点,你必须确保对象在构造调用对象之前完全构造。

Solution you must make sure 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 gurantee that the object has not been destroyed. To do this you must just gurantee 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 gurantee 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.
        }
};

这篇关于单例死引用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆