2

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

bikram990
  • 1,085
  • 1
  • 14
  • 36
  • 2
    Shared libraries only share the code, not the data. Each executable gets its own data, so you will have two singletons when you execute two executables. – Skizz Nov 08 '12 at 10:31
  • The constructor and destructor of the singleton object is called twice even if one executable was running. – bikram990 Nov 08 '12 at 10:36
  • 2
    A doubleton? Sounds like a bug. It will be really hard to figure out without any code. – R. Martinho Fernandes Nov 08 '12 at 10:36
  • 1
    Are you using Meyer's Singleton pattern in multi-threaded code (pre C++11) ? – hmjd Nov 08 '12 at 10:40
  • I think what is discussed on url is happening: http://stackoverflow.com/questions/1271248/c-when-and-how-are-c-global-static-constructors-called – bikram990 Nov 08 '12 at 10:41
  • yes i'm using Meyer's Singleton pattern – bikram990 Nov 08 '12 at 10:43
  • @bikram990, see this http://stackoverflow.com/questions/1661529/is-meyers-implementation-of-singleton-pattern-thread-safe – hmjd Nov 08 '12 at 10:53
  • thanks hmjd got it work with little modifications. – bikram990 Nov 09 '12 at 13:07
  • only the singleton problem is solved – bikram990 Nov 09 '12 at 13:16
  • sorry to post late but it is solved. Thankx to every one – bikram990 Dec 21 '12 at 03:54
  • What did you do to solve it? – Fred Jul 22 '21 at 21:07
  • 1
    @Fred It was a Redhat OS issue where it used to create the singleton objects for loaded libraries and later the OS would merge the memory allocation. I fixed this by using synchronization mechanisms provided by boost library. I don't have the link to the Redhat bug which caused this issue in the first place. Also, this was later fixed by Redhat in version 5 or 6. – bikram990 Aug 11 '21 at 05:32

0 Answers0