Hello folks! I’m using SDL in conjunction with a C++ project and I
was wondering lately, why some of my object destructors aren’t called.
Doing a litte research, I found out that this happens with objects at
global scope due to SDL calling the exit() function after my ‘main’
function returns. On my windows box, the call is made from
main/win32/SDL_win32_main.c’ in function ‘console_main’:
/* Exit cleanly, calling atexit() functions */
exit(status);
Running the following C++ program, the destructor ‘~A()’ for the
global variable’a’ is never called when SDL headers are included:
#include <SDL/SDL.h>
#include
class A
{
public:
A()
{
std::cout << “construct” << std::endl;
}
~A()
{
std::cout << "destruct" << std::endl;
}
};
A a;
int main(int argc, char* argv[])
{
return 0;
}
AFAIK the standard states something like “Automatic objects are
not destroyed as a result of calling exit()”, so ‘~A()’ not being
called is perfectly normal in this case, but i doubt that this is
the desired behaviour when using SDL with a C++ projekt like this
(especially when the destructor does some important cleanup).
Well, i’m still able to do a workaround by running my cleanup code
by registering an additional atexit() function, but i don’t appreciate
such a solution very much since C++ offers a more elegant way by using
destructors (besides the need to take care of every single global
variable manually).
So i was wondering whether it might be possible to refrain from
calling exit() from within the SDL library at all. There might be
a good reason for it, i’m not an expert in SDL’s internals, but
AFAIK without explicitly calling exit(), SDL’s own atexit() fuctions
will still be called when the ‘console_main’ function returns. Maybe
I’m missing something here, but i’d consider SDL not breaking
expected C++ bevaviour to be much more desirable ;o)
Cheers,
Sebastian