Boost serilaization onexit problem
Dear all,
We use (too many) singletons in our program and now the program crashes in
Boost Serialization because in an onexit function a static member
(oserializer::instantiate) of Boost Serialization is already destroyed.
Is there a way to force an intialisation of these static members so that they
are destroyed after my singleton destroys?
The code can be reproduced by the follwing snippet:
//
#define BOOST_ALL_DYN_LINK
#define BOOST_LIB_DIAGNOSTIC
#include <fstream>
#include
I compiled and ran your example on my vc 7.1 environment and was able to reproduce the situation. I modified your main function so that the test worked. int main() { test_serialize(); atexit(&f); return 0; } So I suppose you can get things to work by ensuring that there is a "dummy" serialization before your register with atexit. Personally I wouldn't be comfortable with this as it would depend on undefined behavior and wouldnt guarentee portability. But it might be good enough for your purposes. Your "dummy" serializaton could output to a null stream and just use "register" to make sure all the statics for all the types instantiated. The idea of calling serialization from a function registered with atexit would seem to be a very bad idea to me. Better would be to slightly factor your solution into: class my_main { main(....) ~my_main(){ test_serialize(); } }; main(...){ my_main(...); } This would guarentee that all the serializations occur before the statics are destroyed. Robert Ramey gast128 wrote:
Dear all,
We use (too many) singletons in our program and now the program crashes in Boost Serialization because in an onexit function a static member (oserializer::instantiate) of Boost Serialization is already destroyed.
Hello Robert, the problem is related to use of singleton, which have many problems as described in Alexandrescu. We have singletons a la: class Singleton { public: static Singleton* Instance() { if (ms_pInstance == NULL) { ms_pInstance = new Singleton } return ms_pInstance; } private: Singleton() { atexit(&Destroy); } static void Destroy() { delete ms_pInstance; } static Singleton* ms_pInstance; }; I was hoping for some sort of work around in which the serialization library could be fooled to initialise itself with some simple function calls, but indeed tackling the singleton problem would be more fundamental. wkr, me
participants (2)
-
gast128
-
Robert Ramey