Suggestion: Exit behaviour

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

Why not do something like this instead of using SDL_Quit in your program?

int main(int argc, char *argv[]) {
// …
while(running) {
// do mainloopy things
}

// cleaner exit: stack objects call their destructors and you
// get a chance to do nicer destruction of dynamic memory
// objects

}

…and just set running to false when you want to exit your game.
Using any form of exit(), like you said, doesn’t really lend itself to
pretty code.On 8/1/08, Sebastian Jude <sebastian.jude at st.ovgu.de> wrote:

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


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Madison McGaffin <greyhill gmail.com> writes:

Why not do something like this instead of using SDL_Quit in your program?

int main(int argc, char *argv[]) {
// …
while(running) {
// do mainloopy things
}

// cleaner exit: stack objects call their destructors and you
// get a chance to do nicer destruction of dynamic memory
// objects

}

[…]

Well, the problem is not that I’m calling exit() anywhere in my code, but the
SDL library does, and my suggestion was that it rather shouldn’t. Although your
suggestion is correct, it doesn’t deal with the issue i raised.

Sebastian

Hi,
I think that the destructor is called but you just don’t see it.
If you try to create a global ostream file and log the 2 messages (“construct” and “destruct”) on it, you will find them on this file.

#include

std::ofstream Fout(“output.txt”);
class A
{
public:
A()
{
Fout << “construct” << std::endl;
}

~A()
{
Fout << “destruct” << std::endl;
}
};

A a;

int main(int argc, char* argv[])
{
return 0;
}

But I don’t know why this behaviour.
regards

Dahman

----- Messaggio originale -----Da: “Sebastian Jude” <sebastian.jude at st.ovgu.de>
A: sdl at libsdl.org
Inviato: Venerd?, 1 agosto 2008 14:00:30 GMT +01:00 Amsterdam/Berlino/Berna/Roma/Stoccolma/Vienna
Oggetto: [SDL] Suggestion: Exit behaviour

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


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi,

Your example program doesn’t seem to #include SDL.h. Do you still get
the same behaviour if you do?

Peter

2008/8/1 Abderrahmane Madani <abderrahmane.madani at bncelettronica.com>:> Hi,

I think that the destructor is called but you just don’t see it.
If you try to create a global ostream file and log the 2 messages
(“construct” and “destruct”) on it, you will find them on this file.

#include

std::ofstream Fout(“output.txt”);
class A
{
public:
A()
{
Fout << “construct” << std::endl;
}

     ~A()
     {
        Fout << "destruct" << std::endl;
     }

};

A a;

int main(int argc, char* argv[])
{
return 0;
}

But I don’t know why this behaviour.
regards

Dahman

----- Messaggio originale -----
Da: “Sebastian Jude” <sebastian.jude at st.ovgu.de>
A: sdl at libsdl.org
Inviato: Venerd?, 1 agosto 2008 14:00:30 GMT +01:00
Amsterdam/Berlino/Berna/Roma/Stoccolma/Vienna
Oggetto: [SDL] Suggestion: Exit behaviour

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


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Abderrahmane Madani <abderrahmane.madani bncelettronica.com> writes:

Hi,I think that the destructor is called but you just don’t see it.
If you try to create a global ostream file and log the 2 messages
(“construct” and “destruct”) on it, you will find them on this file.

Oh dear, I hadn’t thought of that - vanishes in a puff of logic … now thats
interesting! Maybe i’s got something to do with SDL’s redirection of console
output - although i disabled it.

Well, that solves the problem for me. I can live with the missing output, the
destructors won’t print out messages anyway. Thanks!

Sebastian

Peter Mackay <mackay.pete+sdl gmail.com> writes:

Hi,

Your example program doesn’t seem to #include SDL.h. Do you still get
the same behaviour if you do?

It does. The missing include also caught my eye, but i just tried it with SDL.h
and the file still contained the destructor output.

Peter

Sebastian

Yes. I just forgot to include it on my message.
Regrads

Dahman

----- Messaggio originale -----Da: “Peter Mackay” <mackay.pete+sdl at gmail.com>
A: “A list for developers using the SDL library. (includes SDL-announce)”
Inviato: Venerd?, 1 agosto 2008 16:02:43 GMT +01:00 Amsterdam/Berlino/Berna/Roma/Stoccolma/Vienna
Oggetto: Re: [SDL] R: Suggestion: Exit behaviour

Hi,

Your example program doesn’t seem to #include SDL.h. Do you still get
the same behaviour if you do?

Peter

2008/8/1 Abderrahmane Madani <abderrahmane.madani at bncelettronica.com>:

Hi,
I think that the destructor is called but you just don’t see it.
If you try to create a global ostream file and log the 2 messages
(“construct” and “destruct”) on it, you will find them on this file.

#include

std::ofstream Fout(“output.txt”);
class A
{
public:
A()
{
Fout << “construct” << std::endl;
}

~A()
{
Fout << “destruct” << std::endl;
}
};

A a;

int main(int argc, char* argv[])
{
return 0;
}

But I don’t know why this behaviour.
regards

Dahman

----- Messaggio originale -----
Da: “Sebastian Jude” <sebastian.jude at st.ovgu.de>
A: sdl at libsdl.org
Inviato: Venerd?, 1 agosto 2008 14:00:30 GMT +01:00
Amsterdam/Berlino/Berna/Roma/Stoccolma/Vienna
Oggetto: [SDL] Suggestion: Exit behaviour

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


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I think the issue has to do with trying to maintain C compatibility.
Somewhat facetiously, if you really wanted a tailored for C++ version,
the source is out there for you to modify and re-release. Personally,
I use the SDL_Quit event to handle these issues.

JohnOn Fri, Aug 1, 2008 at 8:46 AM, Sebastian Jude <sebastian.jude at st.ovgu.de> wrote:

Madison McGaffin <greyhill gmail.com> writes:

Why not do something like this instead of using SDL_Quit in your program?

int main(int argc, char *argv[]) {
// …
while(running) {
// do mainloopy things
}

// cleaner exit: stack objects call their destructors and you
// get a chance to do nicer destruction of dynamic memory
// objects

}

[…]

Well, the problem is not that I’m calling exit() anywhere in my code, but the
SDL library does, and my suggestion was that it rather shouldn’t. Although your
suggestion is correct, it doesn’t deal with the issue i raised.

Why not do something like this instead of using SDL_Quit in your program?

I don’t see any usage of SDL_Quit() in his code, and SDL_Quit has
nothing to do with it anyway.

I agree with the original poster, this seems to be a problem. On
Linux, for example, this would work as expected. I have no idea why
the console_main calls exit() with the return value, instead of
returning it (which should normally call the atexit() functions,
assuming Windows/Visual C works according to standard C).

Another weird thing is that it arranges for SDL_Quit to be called from
an atexit(), which is specifically discouraged by the SDL FAQ. In this
particular case, it is safe, but I could envision a Windows developer
assuming that this is normal, and not calling SDL_Quit() himself,
which would yield an incorrect program on other platforms…On Fri, Aug 1, 2008 at 9:27 AM, Madison McGaffin wrote:


http://pphaneuf.livejournal.com/

Why is SDL not using “return” and using “exit”? It should return a value.
They way I see it, SDL is a library so it should return back to the function
that called it and not to the system.–
Rogelio Nodal