Skip to content

Commit

Permalink
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn…
Browse files Browse the repository at this point in the history
…'t safe

 if SDL is built with a non-cdecl calling convention, and it's just generally
 bad practice anyhow.

Now programs explicitly call SDL_Quit() where appropriate, wrap SDL_Quit() in
 a cdecl function where it can't be avoided, and rely on the parachute where
 a crash might have hit the atexit() before (these ARE test programs, after
 all!).
  • Loading branch information
icculus committed Sep 28, 2005
1 parent a452729 commit b77cfd6
Show file tree
Hide file tree
Showing 20 changed files with 272 additions and 129 deletions.
16 changes: 12 additions & 4 deletions test/checkkeys.c
Expand Up @@ -10,6 +10,13 @@

#include "SDL.h"

/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}

static void print_modifiers(void)
{
int mod;
Expand Down Expand Up @@ -82,9 +89,8 @@ int main(int argc, char *argv[])
/* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1);
return(1);
}
atexit(SDL_Quit);

videoflags = SDL_SWSURFACE;
while( argc > 1 ) {
Expand All @@ -93,15 +99,15 @@ int main(int argc, char *argv[])
videoflags |= SDL_FULLSCREEN;
} else {
fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
exit(1);
quit(1);
}
}

/* Set 640x480 video mode */
if ( SDL_SetVideoMode(640, 480, 0, videoflags) == NULL ) {
fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
SDL_GetError());
exit(2);
quit(2);
}

/* Enable UNICODE translation for keyboard input */
Expand Down Expand Up @@ -132,5 +138,7 @@ int main(int argc, char *argv[])
break;
}
}

SDL_Quit();
return(0);
}
19 changes: 14 additions & 5 deletions test/loopwave.c
Expand Up @@ -19,6 +19,15 @@ struct {
int soundpos; /* Current play position */
} wave;


/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}


void fillerup(void *unused, Uint8 *stream, int len)
{
Uint8 *waveptr;
Expand Down Expand Up @@ -54,21 +63,20 @@ int main(int argc, char *argv[])
/* Load the SDL library */
if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1);
return(1);
}
atexit(SDL_Quit);

if ( argv[1] == NULL ) {
fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]);
exit(1);
quit(1);
}

/* Load the wave file into memory */
if ( SDL_LoadWAV(argv[1],
&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
fprintf(stderr, "Couldn't load %s: %s\n",
argv[1], SDL_GetError());
exit(1);
quit(1);
}
wave.spec.callback = fillerup;

Expand All @@ -86,7 +94,7 @@ int main(int argc, char *argv[])
if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
SDL_FreeWAV(wave.sound);
exit(2);
quit(2);
}
SDL_PauseAudio(0);

Expand All @@ -98,5 +106,6 @@ int main(int argc, char *argv[])
/* Clean up on signal */
SDL_CloseAudio();
SDL_FreeWAV(wave.sound);
SDL_Quit();
return(0);
}
23 changes: 16 additions & 7 deletions test/testalpha.c
Expand Up @@ -12,6 +12,14 @@

#define FRAME_TICKS (1000/30) /* 30 frames/second */

/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}


/* Create a "light" -- a yellowish surface with variable alpha */
SDL_Surface *CreateLight(SDL_Surface *screen, int radius)
{
Expand Down Expand Up @@ -292,9 +300,8 @@ int main(int argc, char *argv[])
/* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1);
return(1);
}
atexit(SDL_Quit);

/* Alpha blending doesn't work well at 8-bit color */
info = SDL_GetVideoInfo();
Expand Down Expand Up @@ -327,22 +334,22 @@ int main(int argc, char *argv[])
fprintf(stderr,
"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
argv[0]);
exit(1);
quit(1);
}
}

/* Set 640x480 video mode */
if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
video_bpp, SDL_GetError());
exit(2);
quit(2);
}

/* Set the surface pixels and refresh! */
if ( SDL_LockSurface(screen) < 0 ) {
fprintf(stderr, "Couldn't lock the display surface: %s\n",
SDL_GetError());
exit(2);
quit(2);
}
buffer=(Uint8 *)screen->pixels;
if (screen->format->BytesPerPixel!=2) {
Expand Down Expand Up @@ -371,13 +378,13 @@ int main(int argc, char *argv[])
/* Create the light */
light = CreateLight(screen, 82);
if ( light == NULL ) {
exit(1);
quit(1);
}

/* Load the sprite */
if ( LoadSprite(screen, "icon.bmp") < 0 ) {
SDL_FreeSurface(light);
exit(1);
quit(1);
}

/* Print out information about our surfaces */
Expand Down Expand Up @@ -492,5 +499,7 @@ fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks));
printf("%d alpha blits, ~%4.4f ms per blit\n",
flashes, (float)flashtime/flashes);
}

SDL_Quit();
return(0);
}
19 changes: 13 additions & 6 deletions test/testbitmap.c
Expand Up @@ -8,6 +8,13 @@
#include "SDL.h"
#include "picture.xbm"

/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}

SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits)
{
SDL_Surface *bitmap;
Expand Down Expand Up @@ -61,9 +68,8 @@ int main(int argc, char *argv[])
/* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1);
return(1);
}
atexit(SDL_Quit);

video_bpp = 0;
videoflags = SDL_SWSURFACE;
Expand All @@ -85,15 +91,15 @@ int main(int argc, char *argv[])
fprintf(stderr,
"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
argv[0]);
exit(1);
quit(1);
}
}

/* Set 640x480 video mode */
if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
video_bpp, SDL_GetError());
exit(2);
quit(2);
}

if (video_bpp==8) {
Expand All @@ -110,7 +116,7 @@ int main(int argc, char *argv[])
if ( SDL_LockSurface(screen) < 0 ) {
fprintf(stderr, "Couldn't lock the display surface: %s\n",
SDL_GetError());
exit(2);
quit(2);
}
buffer=(Uint8 *)screen->pixels;
if (screen->format->BytesPerPixel!=2) {
Expand Down Expand Up @@ -139,7 +145,7 @@ int main(int argc, char *argv[])
bitmap = LoadXBM(screen, picture_width, picture_height,
(Uint8 *)picture_bits);
if ( bitmap == NULL ) {
exit(1);
quit(1);
}

/* Wait for a keystroke */
Expand Down Expand Up @@ -173,5 +179,6 @@ int main(int argc, char *argv[])
}
}
SDL_FreeSurface(bitmap);
SDL_Quit();
return(0);
}
16 changes: 11 additions & 5 deletions test/testcdrom.c
Expand Up @@ -7,6 +7,12 @@

#include "SDL.h"

/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}

static void PrintStatus(int driveindex, SDL_CD *cdrom)
{
Expand Down Expand Up @@ -92,14 +98,13 @@ int main(int argc, char *argv[])
/* Initialize SDL first */
if ( SDL_Init(SDL_INIT_CDROM) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1);
return(1);
}
atexit(SDL_Quit);

/* Find out how many CD-ROM drives are connected to the system */
if ( SDL_CDNumDrives() == 0 ) {
printf("No CD-ROM devices detected\n");
exit(0);
quit(0);
}
printf("Drives available: %d\n", SDL_CDNumDrives());
for ( i=0; i<SDL_CDNumDrives(); ++i ) {
Expand All @@ -116,7 +121,7 @@ int main(int argc, char *argv[])
if ( cdrom == NULL ) {
fprintf(stderr, "Couldn't open drive %d: %s\n", drive,
SDL_GetError());
exit(2);
quit(2);
}
#ifdef TEST_NULLCD
cdrom = NULL;
Expand Down Expand Up @@ -192,11 +197,12 @@ int main(int argc, char *argv[])
} else {
PrintUsage(argv[0]);
SDL_CDClose(cdrom);
exit(1);
quit(1);
}
}
PrintStatus(drive, cdrom);
SDL_CDClose(cdrom);
SDL_Quit();

return(0);
}
19 changes: 12 additions & 7 deletions test/testdyngl.c
Expand Up @@ -23,6 +23,13 @@

#include "SDL_opengl.h"

/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}

void* get_funcaddr(const char* p)
{
void* f=SDL_GL_GetProcAddress(p);
Expand All @@ -33,7 +40,7 @@ void* get_funcaddr(const char* p)
else
{
printf("Unable to get function pointer for %s\n",p);
exit(1);
quit(1);
}
}

Expand Down Expand Up @@ -104,27 +111,25 @@ int main(int argc,char *argv[])
if (SDL_Init(SDL_INIT_VIDEO)<0)
{
printf("Unable to init SDL : %s\n",SDL_GetError());
exit(1);
return(1);
}

atexit(SDL_Quit);

if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
{
printf("Unable to set GL attribute : %s\n",SDL_GetError());
exit(1);
quit(1);
}

if (SDL_GL_LoadLibrary(gl_library)<0)
{
printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
exit(1);
quit(1);
}

if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
{
printf("Unable to open video mode : %s\n",SDL_GetError());
exit(1);
quit(1);
}

/* Set the window manager title bar */
Expand Down

0 comments on commit b77cfd6

Please sign in to comment.