单例对象两次的构造函数和析构函数 [英] Constructor and Destructor of a singleton object called twice

查看:170
本文介绍了单例对象两次的构造函数和析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++中的单例对象中遇到问题.解释如下:

问题信息:我有一个4个共享库(例如libA.so,libB.so,libC.so,libD.so)和2个可执行二进制文件,每个文件都使用另一个处理文件的共享库(例如libE.so). br/>
libE.so的目的是将数据写入文件,并且如果可执行文件重新启动或文件大小超过某个限制,则将其压缩并创建带有名称时间戳的新文件.它正在使用单例对象.它导出用于获取和使用单例的处理程序类.压缩仅在上述两种情况下发生.用户/加载程序可执行文件可以指定文件的起始名称,只有处理程序类没有提供其他控件.

libA.so,libB.so,libC.so和libD.so具有几乎相同的行为.它们都具有一个处理程序的类,声明和对象,该处理程序在libE.so中获取单例的实例并将其用于其他目的.

所有这些库都链接到两个可执行的二进制文件.如果两个可执行文件中只有一个可执行文件,则可以正常运行;但是,如果两个可执行文件都一个接一个地运行,则第一个启动的可执行文件的文件将被压缩.

调试信息:单例对象的构造函数和析构函数被调用两次.(对于每个可执行文件)

单例的对象是静态对象,不会删除.

可执行文件无法退出/返回给定:

*检测到glibc *(exe1或exe2):双重释放或损坏(!prev):some_addr *

使用二进制valgrind运行时,得出上述错误是由于单例对象的析构函数所致.

感谢

I''m facing a problem in singleton object in c++. Here is the explanation:

Problem info: I have a 4 shared libraries (say libA.so, libB.so, libC.so, libD.so) and 2 executable binary files each using one another shared library( say libE.so) which deals with files.

The purpose of libE.so is to write data into a file and if the executable restarts or size of file exceeds a certain limit it is zipped and a new file is created with time stamp in name. It is using singleton object. It exports a handler class for getting and using singleton. Compressing only happens in the above said two cases. The user/loader executable can specify the starting name of file only no other control is provided by handler class.

libA.so, libB.so, libC.so and libD.so have almost same behavior. They all have a class and declare and object of an handler which gets the instance of the singleton in libE.so and uses it for further purpose.

All these libraries are linked to two executable binary files. If only one of the two executable runs then its fine, But if both executable runs one after other then the file of the first started executable gets compressed.

Debug info: The constructor and destructor of the singleton object is called twice.(for each executable)

The object of singleton is a static object and never deleted.

The executable is not able to exit/return gives:

* glibc detected * (exe1 or exe2): double free or corruption (!prev): some_addr *

Running with binaries valgrind gives that the above error is due to the destructor of the singleton object.

Thanks

推荐答案

可能是您的链接器配置错误.尝试在链接器命令行中添加 -rdynamic .

请参考解释此行为的以下答案:
http://stackoverflow.com/questions/8623657/multiple- Linux上的instances-of-singleton-across-shared-libraries [
It may be that your linker is configured the wrong way. Try adding -rdynamic to the linker command line.

Refer to this answer which explains this behaviour:
http://stackoverflow.com/questions/8623657/multiple-instances-of-singleton-across-shared-libraries-on-linux[^]


这篇关于单例对象两次的构造函数和析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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