Original Post
originally working with SDL on Mac, I've found a strange anomaly after importing my project to Dev-Cpp under a BootCamp Windows XP. for my game subsystems (that mostly wrap SDL) I use singletonized classes (SingletonHolder<Subsystem>), which sit in a namespace and are identified as references. some relevant code: Now, the anomaly is that in the windows version the singletonized classes' destructors are never called (their shutdown isn't logged) - whereas the OSX version works like a charm. This had been like that since forever, and didn't seem to cause much trouble. The more severe problem is that, for a while now, the program will NOT execute twice in the same windows session. It will crash on second execution, so hard, that there's no way to shut it down (after some random time and amount of punching it, it will go away, or will it?), and will likely crash DevCpp. Oh, by the way, the first time this happened was after I've introduced some compilation switches to make it easier to use the same source for Mac and Win, such as: Now this doesn't make sense at all. Or does it? My two cents would be that this has to do with SDL not being shut down properly (so I'm not saying the OSX version is perfect, maybe the OS takes care of whatever is in the background). So first, I've got rid of atexit and called SDL_Quit() from ~Core(), then tried to do without the singletons, but the problem remained the same. As far as I know, other SDL applications (say, G++ ones rather than VC++) don't have this sort of problem. Does anybody know what's causing this? Actually, as I'm writing this, I get the idea that the windows version might be shutting LJ::LOG down early, then, when the other subsystems' destructor want to call it, it's not there, they crash, and SDL_Quit is never called? Could this be it?
// singletonholder
template <typename T>
class SingletonHolder
{
public:
static T& Instance() {
static T *pInst = new T;
return pInst;
}
private:
SingletonHolder();
SingletonHolder(const SingletonHolder &rhs);
SingletonHolder& operator=(const SingletonHolder &rhs);
~SingletonHolder();
};
// core.hpp
class Core
{
public:
Core(Uint32 flags = SDL_INIT_EVERYTHING);
~Core();
};
// core.cpp
Core::Core(Uint32 flags) {
atexit(SDL_Quit);
if(::SDL_Init(flags) < 0) {
exit(1);
}
LJ::LOG << "Core was succesfully initialized.";
}
Core::~Core() {
LJ::LOG << "Core is shutting down";
}
// framework.hpp
namespace LJ
{
extern class Log LOG; // not a singleton
extern class Core &CORE;
};
// framework.cpp
namespace LJ
{
Log LOG("system.log");
Core &CORE(SingletonHolder<Core>::Instance());
};
#if defined(__APPLE__)
#include "SDL.h"
#include "SDL_opengl.h"
#include "SDL_image/SDL_image.h"
#include "SDL_mixer/SDL_mixer.h"
#elif defined(__WIN32__)
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#endif