Meyers Singleton的破坏早于最后使用
我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,破坏的顺序是建筑的逆转。因此,您必须确保在使用它的另一个静态对象之前初始化
singleton
(我们称其为singletonclient
)。所以,这样的事情: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 itSingletonClient
). So, something like this: