Meyers Singleton的破坏早于最后使用

发布于 2025-01-25 00:57:07 字数 786 浏览 1 评论 0原文

我在Ubuntu上的Dynamic Library A.So中遇到了Meyers Singleton的问题:

class Singleton
{
/*some functionality*/
}

Singleton& getSingleton(); // in header file

Singleton& getSingleton() // in .cpp file
{
    static Singleton value;
    return value;
}

function getsingleton()在类型的对象的构造函数和deStructor 用户 of <<强>其他库

class User
{
    User()
    {
        getSingleton().addSmth();
    }
    ~User()
    {
        getSingleton().removeSmth();
    }
    /*some other functionality*/
};

B.

因此,我面对singleton对象 从其他用户 class destructor否决。

我的项目有多个库,它是使用Cmake和GCC-10.3构建的,Ubuntu 20.04。我需要帮助修复Singleton的Object Lifetime,

我在Windows 10上检查了同一项目,并且效果很好

I faced problem with Meyers singleton implemented this way in dynamic library A.so on Ubuntu:

class Singleton
{
/*some functionality*/
}

Singleton& getSingleton(); // in header file

Singleton& getSingleton() // in .cpp file
{
    static Singleton value;
    return value;
}

And function getSingleton() is called in constructor and destructor of object of type User of other library B.so, like this:

class User
{
    User()
    {
        getSingleton().addSmth();
    }
    ~User()
    {
        getSingleton().removeSmth();
    }
    /*some other functionality*/
};

Object of class User is created as static object of some function in B.so.

So, I faced with destroying Singleton object before calling it from other User class's destructor.

My project has multiple libraries, it is built with CMake and gcc-10.3 on Ubuntu 20.04. I need help with fixing of Singleton's object lifetime

I checked the same project in Visual Studio on Windows 10 and it works well

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2025-02-01 00:57:07

请记住,破坏的顺序是建筑的逆转。因此,您必须确保在使用它的另一个静态对象之前初始化singleton(我们称其为singletonclient)。所以,这样的事情:

struct SingletonClient {
  SingletonClient() {
    getSingleton(); // makes sure the other object is constructed before this one
  }
};

Keep in mind that the order of destruction is the reverse of construction. So you must make sure Singleton is initialized before the other static object that's using it (let's call it SingletonClient). So, something like this:

struct SingletonClient {
  SingletonClient() {
    getSingleton(); // makes sure the other object is constructed before this one
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文