SDL_main.c ... problems with stdout/stderr

Hi!

I’m using SDL 1.2.5 (Win32) and was a bit surprised that all my debug
output/logging to a console window disappeared when I linked with
SDLmain.lib! The output was caused by a global variable which went out
of scope at the end of the program (and basically shuts down my entire
framework … heavily smart-pointer laced stuff). I browsed throught the
source and noticed that in SDL_main.c there is a little innocent
function called cleanup_output(). This gets called before my global
reference goes out of scope and it closes stderr and stdout … causing
all other writes to those to fail. So all the output, which I do after
that point does not show anymore. Wouldn’t it be better to ‘fflush’ the
streams instead of ‘fclose’ them?

-Marco

Hi!

I’m using SDL 1.2.5 (Win32) and was a bit surprised that all my debug
output/logging to a console window disappeared when I linked with
SDLmain.lib!

SDL is dumping stderr/stdout to the files stderr.txt and stdout.txt

The output was caused by a global variable which went out of scope at
the end of the program (and basically shuts down my entire framework
… heavily smart-pointer laced stuff). I browsed throught the source
and noticed that in SDL_main.c there is a little innocent function
called cleanup_output(). This gets called before my global reference
goes out of scope and it closes stderr and stdout … causing all
other writes to those to fail.

It seems cleanup_output() is called by exit()…so if you don’t
shutdown your program with exit() you should be OK. You can shutdown by
returning from your main() (which is actually SDL_main)

So all the output, which I do after that point does not show anymore.
Wouldn’t it be better to ‘fflush’ the streams instead of ‘fclose’ > them?

I suspect that if you don’t fclose(), the OS might think there are
still 2 open files when the program exits, so this might be a no-no.

HTH,
DarrellOn Thursday, October 10, 2002, at 05:20 AM, Marco K?gler wrote:

Marco K?gler wrote:

I’m using SDL 1.2.5 (Win32) and was a bit surprised that all my
debug
output/logging to a console window disappeared when I linked with
SDLmain.lib! The output was caused by a global variable which went
out
of scope at the end of the program (and basically shuts down my
entire
framework … heavily smart-pointer laced stuff).

A bit of advice: put the variable inside ‘main’ instead. If you need
to access ot outside of ‘main’, use a global pointer. Not only will
this work with SDL, but it’s also safer (since initialization order of
globals across compilation units is unspecified).–
Rainer Deyke | root at rainerdeyke.com | http://rainerdeyke.com

Rainer Deyke wrote:

Marco K?gler wrote:

A bit of advice: put the variable inside ‘main’ instead. If you need
to access ot outside of ‘main’, use a global pointer. Not only will
this work with SDL, but it’s also safer (since initialization order of
globals across compilation units is unspecified).

Well, the global in question is a smart-pointer … I get to choose the
exact moment in question when that pointer holds a reference, so
initialization order is under total control. Destruction order is
’automatic’ with the smart-pointers as well.

-Marco