I'm adding a singleton logging class that everyone will use in our current project. However we're facing a problem - the singleton global object has to be destructed VERY LAST because any other object can log in their destructor. If the singleton gets destroyed first, and some other global object calls the log function, it will lead to segmentation fault as logger has been destructed. Does anyone know if it's somehow possible to destruct an object very last int he program? I think that is not possible, so does anyone know a workaround in my particular case?
Asked
Active
Viewed 98 times
1
-
1This is why you shouldn't use singletons or global objects. Among [other reasons](http://stackoverflow.com/questions/137975). – Mike Seymour Jul 14 '14 at 14:57
-
I don't know what kind of project it is, but in case of a dll you could create the singleton when loading the dll and destroy it when unloading. – cageman Jul 14 '14 at 14:57
-
2What happens if another object wants to be destroyed VERY LAST? – Jul 14 '14 at 14:57
-
3Try using a library for logging and dont be bothered about its implementation. You can use log4cpp as a good open source implementation. Please visit http://log4cpp.sourceforge.net/ – Pratham Jul 14 '14 at 14:58
-
Which compiler/OS are you using? GCC on linux has a nice attribute for this. – T.C. Jul 14 '14 at 15:01
-
@T.C. I'm using Visual Studio – bodacydo Jul 14 '14 at 15:04
-
@delnan wow.... you're right... – bodacydo Jul 14 '14 at 15:09
-
3This is one of the scenarios where leaking is sometimes an option. The only one who cares whether your object is destroyed last or not at all is usually the leak detector, so this could be the right way to go for you. – ComicSansMS Jul 14 '14 at 15:15