Skip to content
Permalink
Browse files

Removed atexit() from most of the test programs; atexit(SDL_Quit) isn…

…'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 b77cfd625af1620704c3415c01684a95b430688c
Showing with 272 additions and 129 deletions.
  1. +12 −4 test/checkkeys.c
  2. +14 −5 test/loopwave.c
  3. +16 −7 test/testalpha.c
  4. +13 −6 test/testbitmap.c
  5. +11 −5 test/testcdrom.c
  6. +12 −7 test/testdyngl.c
  7. +10 −3 test/testerror.c
  8. +11 −4 test/testgamma.c
  9. +13 −6 test/testhread.c
  10. +10 −1 test/testlock.c
  11. +22 −15 test/testoverlay.c
  12. +32 −25 test/testoverlay2.c
  13. +12 −5 test/testpalette.c
  14. +10 −3 test/testsem.c
  15. +12 −6 test/testsprite.c
  16. +9 −2 test/testtimer.c
  17. +20 −12 test/testwin.c
  18. +11 −4 test/testwm.c
  19. +11 −4 test/threadwin.c
  20. +11 −5 test/torturethread.c
@@ -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;
@@ -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 ) {
@@ -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 */
@@ -132,5 +138,7 @@ int main(int argc, char *argv[])
break;
}
}

SDL_Quit();
return(0);
}
@@ -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;
@@ -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;

@@ -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);

@@ -98,5 +106,6 @@ int main(int argc, char *argv[])
/* Clean up on signal */
SDL_CloseAudio();
SDL_FreeWAV(wave.sound);
SDL_Quit();
return(0);
}
@@ -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)
{
@@ -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();
@@ -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) {
@@ -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 */
@@ -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);
}
@@ -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;
@@ -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;
@@ -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) {
@@ -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) {
@@ -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 */
@@ -173,5 +179,6 @@ int main(int argc, char *argv[])
}
}
SDL_FreeSurface(bitmap);
SDL_Quit();
return(0);
}
@@ -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)
{
@@ -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 ) {
@@ -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;
@@ -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);
}
@@ -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);
@@ -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);
}
}

@@ -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 */

0 comments on commit b77cfd6

Please sign in to comment.