Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
authorRyan C. Gordon <icculus@icculus.org>
Wed, 28 Sep 2005 11:36:20 +0000
changeset 1151be9c9c8f6d53
parent 1150 7d8e1925f35b
child 1152 51a8702d8ecd
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!).
test/checkkeys.c
test/loopwave.c
test/testalpha.c
test/testbitmap.c
test/testcdrom.c
test/testdyngl.c
test/testerror.c
test/testgamma.c
test/testhread.c
test/testlock.c
test/testoverlay.c
test/testoverlay2.c
test/testpalette.c
test/testsem.c
test/testsprite.c
test/testtimer.c
test/testwin.c
test/testwm.c
test/threadwin.c
test/torturethread.c
     1.1 --- a/test/checkkeys.c	Wed Sep 28 06:38:22 2005 +0000
     1.2 +++ b/test/checkkeys.c	Wed Sep 28 11:36:20 2005 +0000
     1.3 @@ -10,6 +10,13 @@
     1.4  
     1.5  #include "SDL.h"
     1.6  
     1.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     1.8 +static void quit(int rc)
     1.9 +{
    1.10 +	SDL_Quit();
    1.11 +	exit(rc);
    1.12 +}
    1.13 +
    1.14  static void print_modifiers(void)
    1.15  {
    1.16  	int mod;
    1.17 @@ -82,9 +89,8 @@
    1.18  	/* Initialize SDL */
    1.19  	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    1.20  		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.21 -		exit(1);
    1.22 +		return(1);
    1.23  	}
    1.24 -	atexit(SDL_Quit);
    1.25  
    1.26  	videoflags = SDL_SWSURFACE;
    1.27  	while( argc > 1 ) {
    1.28 @@ -93,7 +99,7 @@
    1.29  			videoflags |= SDL_FULLSCREEN;
    1.30  		} else {
    1.31  			fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
    1.32 -			exit(1);
    1.33 +			quit(1);
    1.34  		}
    1.35  	}
    1.36  
    1.37 @@ -101,7 +107,7 @@
    1.38  	if ( SDL_SetVideoMode(640, 480, 0, videoflags) == NULL ) {
    1.39  		fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
    1.40  							SDL_GetError());
    1.41 -		exit(2);
    1.42 +		quit(2);
    1.43  	}
    1.44  
    1.45  	/* Enable UNICODE translation for keyboard input */
    1.46 @@ -132,5 +138,7 @@
    1.47  				break;
    1.48  		}
    1.49  	}
    1.50 +
    1.51 +	SDL_Quit();
    1.52  	return(0);
    1.53  }
     2.1 --- a/test/loopwave.c	Wed Sep 28 06:38:22 2005 +0000
     2.2 +++ b/test/loopwave.c	Wed Sep 28 11:36:20 2005 +0000
     2.3 @@ -19,6 +19,15 @@
     2.4  	int      soundpos;		/* Current play position */
     2.5  } wave;
     2.6  
     2.7 +
     2.8 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     2.9 +static void quit(int rc)
    2.10 +{
    2.11 +	SDL_Quit();
    2.12 +	exit(rc);
    2.13 +}
    2.14 +
    2.15 +
    2.16  void fillerup(void *unused, Uint8 *stream, int len)
    2.17  {
    2.18  	Uint8 *waveptr;
    2.19 @@ -54,13 +63,12 @@
    2.20  	/* Load the SDL library */
    2.21  	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
    2.22  		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    2.23 -		exit(1);
    2.24 +		return(1);
    2.25  	}
    2.26 -	atexit(SDL_Quit);
    2.27  
    2.28  	if ( argv[1] == NULL ) {
    2.29  		fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]);
    2.30 -		exit(1);
    2.31 +		quit(1);
    2.32  	}
    2.33  
    2.34  	/* Load the wave file into memory */
    2.35 @@ -68,7 +76,7 @@
    2.36  			&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
    2.37  		fprintf(stderr, "Couldn't load %s: %s\n",
    2.38  						argv[1], SDL_GetError());
    2.39 -		exit(1);
    2.40 +		quit(1);
    2.41  	}
    2.42  	wave.spec.callback = fillerup;
    2.43  
    2.44 @@ -86,7 +94,7 @@
    2.45  	if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
    2.46  		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
    2.47  		SDL_FreeWAV(wave.sound);
    2.48 -		exit(2);
    2.49 +		quit(2);
    2.50  	}
    2.51  	SDL_PauseAudio(0);
    2.52  
    2.53 @@ -98,5 +106,6 @@
    2.54  	/* Clean up on signal */
    2.55  	SDL_CloseAudio();
    2.56  	SDL_FreeWAV(wave.sound);
    2.57 +	SDL_Quit();
    2.58  	return(0);
    2.59  }
     3.1 --- a/test/testalpha.c	Wed Sep 28 06:38:22 2005 +0000
     3.2 +++ b/test/testalpha.c	Wed Sep 28 11:36:20 2005 +0000
     3.3 @@ -12,6 +12,14 @@
     3.4  
     3.5  #define FRAME_TICKS	(1000/30)		/* 30 frames/second */
     3.6  
     3.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     3.8 +static void quit(int rc)
     3.9 +{
    3.10 +	SDL_Quit();
    3.11 +	exit(rc);
    3.12 +}
    3.13 +
    3.14 +
    3.15  /* Create a "light" -- a yellowish surface with variable alpha */
    3.16  SDL_Surface *CreateLight(SDL_Surface *screen, int radius)
    3.17  {
    3.18 @@ -292,9 +300,8 @@
    3.19  	/* Initialize SDL */
    3.20  	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    3.21  		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    3.22 -		exit(1);
    3.23 +		return(1);
    3.24  	}
    3.25 -	atexit(SDL_Quit);
    3.26  
    3.27  	/* Alpha blending doesn't work well at 8-bit color */
    3.28  	info = SDL_GetVideoInfo();
    3.29 @@ -327,7 +334,7 @@
    3.30  			fprintf(stderr, 
    3.31  			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
    3.32  								argv[0]);
    3.33 -			exit(1);
    3.34 +			quit(1);
    3.35  		}
    3.36  	}
    3.37  
    3.38 @@ -335,14 +342,14 @@
    3.39  	if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
    3.40  		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
    3.41  						video_bpp, SDL_GetError());
    3.42 -		exit(2);
    3.43 +		quit(2);
    3.44  	}
    3.45  
    3.46  	/* Set the surface pixels and refresh! */
    3.47  	if ( SDL_LockSurface(screen) < 0 ) {
    3.48  		fprintf(stderr, "Couldn't lock the display surface: %s\n",
    3.49  							SDL_GetError());
    3.50 -		exit(2);
    3.51 +		quit(2);
    3.52  	}
    3.53  	buffer=(Uint8 *)screen->pixels;
    3.54  	if (screen->format->BytesPerPixel!=2) {
    3.55 @@ -371,13 +378,13 @@
    3.56  	/* Create the light */
    3.57  	light = CreateLight(screen, 82);
    3.58  	if ( light == NULL ) {
    3.59 -		exit(1);
    3.60 +		quit(1);
    3.61  	}
    3.62  
    3.63  	/* Load the sprite */
    3.64  	if ( LoadSprite(screen, "icon.bmp") < 0 ) {
    3.65  		SDL_FreeSurface(light);
    3.66 -		exit(1);
    3.67 +		quit(1);
    3.68  	}
    3.69  
    3.70  	/* Print out information about our surfaces */
    3.71 @@ -492,5 +499,7 @@
    3.72  		printf("%d alpha blits, ~%4.4f ms per blit\n", 
    3.73  			flashes, (float)flashtime/flashes);
    3.74  	}
    3.75 +
    3.76 +	SDL_Quit();
    3.77  	return(0);
    3.78  }
     4.1 --- a/test/testbitmap.c	Wed Sep 28 06:38:22 2005 +0000
     4.2 +++ b/test/testbitmap.c	Wed Sep 28 11:36:20 2005 +0000
     4.3 @@ -8,6 +8,13 @@
     4.4  #include "SDL.h"
     4.5  #include "picture.xbm"
     4.6  
     4.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     4.8 +static void quit(int rc)
     4.9 +{
    4.10 +	SDL_Quit();
    4.11 +	exit(rc);
    4.12 +}
    4.13 +
    4.14  SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits)
    4.15  {
    4.16  	SDL_Surface *bitmap;
    4.17 @@ -61,9 +68,8 @@
    4.18  	/* Initialize SDL */
    4.19  	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    4.20  		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    4.21 -		exit(1);
    4.22 +		return(1);
    4.23  	}
    4.24 -	atexit(SDL_Quit);
    4.25  
    4.26  	video_bpp = 0;
    4.27  	videoflags = SDL_SWSURFACE;
    4.28 @@ -85,7 +91,7 @@
    4.29  			fprintf(stderr,
    4.30  			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
    4.31  								argv[0]);
    4.32 -			exit(1);
    4.33 +			quit(1);
    4.34  		}
    4.35  	}
    4.36  
    4.37 @@ -93,7 +99,7 @@
    4.38  	if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
    4.39  		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
    4.40  						video_bpp, SDL_GetError());
    4.41 -		exit(2);
    4.42 +		quit(2);
    4.43  	}
    4.44  
    4.45  	if (video_bpp==8) {
    4.46 @@ -110,7 +116,7 @@
    4.47  	if ( SDL_LockSurface(screen) < 0 ) {
    4.48  		fprintf(stderr, "Couldn't lock the display surface: %s\n",
    4.49  							SDL_GetError());
    4.50 -		exit(2);
    4.51 +		quit(2);
    4.52  	}
    4.53  	buffer=(Uint8 *)screen->pixels;
    4.54  	if (screen->format->BytesPerPixel!=2) {
    4.55 @@ -139,7 +145,7 @@
    4.56  	bitmap = LoadXBM(screen, picture_width, picture_height,
    4.57  					(Uint8 *)picture_bits);
    4.58  	if ( bitmap == NULL ) {
    4.59 -		exit(1);
    4.60 +		quit(1);
    4.61  	}
    4.62  
    4.63  	/* Wait for a keystroke */
    4.64 @@ -173,5 +179,6 @@
    4.65  		}
    4.66  	}
    4.67  	SDL_FreeSurface(bitmap);
    4.68 +	SDL_Quit();
    4.69  	return(0);
    4.70  }
     5.1 --- a/test/testcdrom.c	Wed Sep 28 06:38:22 2005 +0000
     5.2 +++ b/test/testcdrom.c	Wed Sep 28 11:36:20 2005 +0000
     5.3 @@ -7,6 +7,12 @@
     5.4  
     5.5  #include "SDL.h"
     5.6  
     5.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     5.8 +static void quit(int rc)
     5.9 +{
    5.10 +	SDL_Quit();
    5.11 +	exit(rc);
    5.12 +}
    5.13  
    5.14  static void PrintStatus(int driveindex, SDL_CD *cdrom)
    5.15  {
    5.16 @@ -92,14 +98,13 @@
    5.17  	/* Initialize SDL first */
    5.18  	if ( SDL_Init(SDL_INIT_CDROM) < 0 ) {
    5.19  		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    5.20 -		exit(1);
    5.21 +		return(1);
    5.22  	}
    5.23 -	atexit(SDL_Quit);
    5.24  
    5.25  	/* Find out how many CD-ROM drives are connected to the system */
    5.26  	if ( SDL_CDNumDrives() == 0 ) {
    5.27  		printf("No CD-ROM devices detected\n");
    5.28 -		exit(0);
    5.29 +		quit(0);
    5.30  	}
    5.31  	printf("Drives available: %d\n", SDL_CDNumDrives());
    5.32  	for ( i=0; i<SDL_CDNumDrives(); ++i ) {
    5.33 @@ -116,7 +121,7 @@
    5.34  	if ( cdrom == NULL ) {
    5.35  		fprintf(stderr, "Couldn't open drive %d: %s\n", drive,
    5.36  							SDL_GetError());
    5.37 -		exit(2);
    5.38 +		quit(2);
    5.39  	}
    5.40  #ifdef TEST_NULLCD
    5.41  	cdrom = NULL;
    5.42 @@ -192,11 +197,12 @@
    5.43  		} else {
    5.44  			PrintUsage(argv[0]);
    5.45  			SDL_CDClose(cdrom);
    5.46 -			exit(1);
    5.47 +			quit(1);
    5.48  		}
    5.49  	}
    5.50  	PrintStatus(drive, cdrom);
    5.51  	SDL_CDClose(cdrom);
    5.52 +	SDL_Quit();
    5.53  
    5.54  	return(0);
    5.55  }
     6.1 --- a/test/testdyngl.c	Wed Sep 28 06:38:22 2005 +0000
     6.2 +++ b/test/testdyngl.c	Wed Sep 28 11:36:20 2005 +0000
     6.3 @@ -23,6 +23,13 @@
     6.4  
     6.5  #include "SDL_opengl.h"
     6.6  
     6.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     6.8 +static void quit(int rc)
     6.9 +{
    6.10 +	SDL_Quit();
    6.11 +	exit(rc);
    6.12 +}
    6.13 +
    6.14  void* get_funcaddr(const char* p)
    6.15  {
    6.16  	void* f=SDL_GL_GetProcAddress(p);
    6.17 @@ -33,7 +40,7 @@
    6.18  	else
    6.19  	{
    6.20  		printf("Unable to get function pointer for %s\n",p);
    6.21 -		exit(1);
    6.22 +		quit(1);
    6.23  	}
    6.24  }
    6.25  
    6.26 @@ -104,27 +111,25 @@
    6.27  	if (SDL_Init(SDL_INIT_VIDEO)<0)
    6.28  	{
    6.29  		printf("Unable to init SDL : %s\n",SDL_GetError());
    6.30 -		exit(1);
    6.31 +		return(1);
    6.32  	}
    6.33  
    6.34 -	atexit(SDL_Quit);
    6.35 -	
    6.36  	if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
    6.37  	{
    6.38  		printf("Unable to set GL attribute : %s\n",SDL_GetError());
    6.39 -		exit(1);
    6.40 +		quit(1);
    6.41  	}
    6.42  	
    6.43  	if (SDL_GL_LoadLibrary(gl_library)<0)
    6.44  	{
    6.45  		printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
    6.46 -		exit(1);
    6.47 +		quit(1);
    6.48  	}
    6.49  
    6.50  	if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
    6.51  	{
    6.52  		printf("Unable to open video mode : %s\n",SDL_GetError());
    6.53 -		exit(1);
    6.54 +		quit(1);
    6.55  	}
    6.56  
    6.57  	/* Set the window manager title bar */
     7.1 --- a/test/testerror.c	Wed Sep 28 06:38:22 2005 +0000
     7.2 +++ b/test/testerror.c	Wed Sep 28 11:36:20 2005 +0000
     7.3 @@ -10,6 +10,13 @@
     7.4  
     7.5  static int alive = 0;
     7.6  
     7.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     7.8 +static void quit(int rc)
     7.9 +{
    7.10 +	SDL_Quit();
    7.11 +	exit(rc);
    7.12 +}
    7.13 +
    7.14  int ThreadFunc(void *data)
    7.15  {
    7.16  	/* Set the child thread error string */
    7.17 @@ -30,9 +37,8 @@
    7.18  	/* Load the SDL library */
    7.19  	if ( SDL_Init(0) < 0 ) {
    7.20  		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    7.21 -		exit(1);
    7.22 +		return(1);
    7.23  	}
    7.24 -	atexit(SDL_Quit);
    7.25  
    7.26  	/* Set the error value for the main thread */
    7.27  	SDL_SetError("No worries");
    7.28 @@ -41,7 +47,7 @@
    7.29  	thread = SDL_CreateThread(ThreadFunc, "#1");
    7.30  	if ( thread == NULL ) {
    7.31  		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    7.32 -		exit(1);
    7.33 +		quit(1);
    7.34  	}
    7.35  	SDL_Delay(5*1000);
    7.36  	printf("Waiting for thread #1\n");
    7.37 @@ -50,5 +56,6 @@
    7.38  
    7.39  	printf("Main thread error string: %s\n", SDL_GetError());
    7.40  
    7.41 +	SDL_Quit();
    7.42  	return(0);
    7.43  }
     8.1 --- a/test/testgamma.c	Wed Sep 28 06:38:22 2005 +0000
     8.2 +++ b/test/testgamma.c	Wed Sep 28 11:36:20 2005 +0000
     8.3 @@ -8,6 +8,13 @@
     8.4  
     8.5  #include "SDL.h"
     8.6  
     8.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     8.8 +static void quit(int rc)
     8.9 +{
    8.10 +	SDL_Quit();
    8.11 +	exit(rc);
    8.12 +}
    8.13 +
    8.14  /* Turn a normal gamma value into an appropriate gamma ramp */
    8.15  void CalculateGamma(double gamma, Uint16 *ramp)
    8.16  {
    8.17 @@ -82,16 +89,15 @@
    8.18  	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    8.19  		fprintf(stderr,
    8.20  			"Couldn't initialize SDL: %s\n", SDL_GetError());
    8.21 -		exit(1);
    8.22 +		return(1);
    8.23  	}
    8.24 -	atexit(SDL_Quit);
    8.25  
    8.26  	/* Initialize the display, always use hardware palette */
    8.27  	screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE);
    8.28  	if ( screen == NULL ) {
    8.29  		fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
    8.30  						w, h, SDL_GetError());
    8.31 -		exit(1);
    8.32 +		quit(1);
    8.33  	}
    8.34  
    8.35  	/* Set the window manager title bar */
    8.36 @@ -104,7 +110,7 @@
    8.37  	}
    8.38  	if ( SDL_SetGamma(gamma, gamma, gamma) < 0 ) {
    8.39  		fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError());
    8.40 -		exit(1);
    8.41 +		quit(1);
    8.42  	}
    8.43  
    8.44  #if 0 /* This isn't supported.  Integrating the gamma ramps isn't exact */
    8.45 @@ -186,5 +192,6 @@
    8.46  	}
    8.47  	SDL_Delay(1*1000);
    8.48  
    8.49 +	SDL_Quit();
    8.50  	return(0);
    8.51  }
     9.1 --- a/test/testhread.c	Wed Sep 28 06:38:22 2005 +0000
     9.2 +++ b/test/testhread.c	Wed Sep 28 11:36:20 2005 +0000
     9.3 @@ -10,6 +10,13 @@
     9.4  
     9.5  static int alive = 0;
     9.6  
     9.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     9.8 +static void quit(int rc)
     9.9 +{
    9.10 +	SDL_Quit();
    9.11 +	exit(rc);
    9.12 +}
    9.13 +
    9.14  int ThreadFunc(void *data)
    9.15  {
    9.16  	printf("Started thread %s: My thread id is %u\n",
    9.17 @@ -27,7 +34,7 @@
    9.18  	printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
    9.19  	SDL_Delay(5*1000);
    9.20  	alive = 0;
    9.21 -	exit(0);
    9.22 +	quit(0);
    9.23  }
    9.24  
    9.25  int main(int argc, char *argv[])
    9.26 @@ -37,15 +44,14 @@
    9.27  	/* Load the SDL library */
    9.28  	if ( SDL_Init(0) < 0 ) {
    9.29  		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    9.30 -		exit(1);
    9.31 +		return(1);
    9.32  	}
    9.33 -	atexit(SDL_Quit);
    9.34  
    9.35  	alive = 1;
    9.36  	thread = SDL_CreateThread(ThreadFunc, "#1");
    9.37  	if ( thread == NULL ) {
    9.38  		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    9.39 -		exit(1);
    9.40 +		quit(1);
    9.41  	}
    9.42  	SDL_Delay(5*1000);
    9.43  	printf("Waiting for thread #1\n");
    9.44 @@ -56,7 +62,7 @@
    9.45  	thread = SDL_CreateThread(ThreadFunc, "#2");
    9.46  	if ( thread == NULL ) {
    9.47  		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    9.48 -		exit(1);
    9.49 +		quit(1);
    9.50  	}
    9.51  	SDL_Delay(5*1000);
    9.52  	printf("Killing thread #2\n");
    9.53 @@ -67,9 +73,10 @@
    9.54  	thread = SDL_CreateThread(ThreadFunc, "#3");
    9.55  	if ( thread == NULL ) {
    9.56  		fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    9.57 -		exit(1);
    9.58 +		quit(1);
    9.59  	}
    9.60  	raise(SIGTERM);
    9.61  
    9.62 +	SDL_Quit();	/* Never reached */
    9.63  	return(0);	/* Never reached */
    9.64  }
    10.1 --- a/test/testlock.c	Wed Sep 28 06:38:22 2005 +0000
    10.2 +++ b/test/testlock.c	Wed Sep 28 11:36:20 2005 +0000
    10.3 @@ -15,6 +15,15 @@
    10.4  static Uint32 mainthread;
    10.5  static SDL_Thread *threads[6];
    10.6  
    10.7 +/*
    10.8 + * SDL_Quit() shouldn't be used with atexit() directly because
    10.9 + *  calling conventions may differ...
   10.10 + */
   10.11 +static void SDL_Quit_Wrapper(void)
   10.12 +{
   10.13 +	SDL_Quit();
   10.14 +}
   10.15 +
   10.16  void printid(void)
   10.17  {
   10.18  	printf("Process %u:  exiting\n", SDL_ThreadID());
   10.19 @@ -68,7 +77,7 @@
   10.20  		fprintf(stderr, "%s\n", SDL_GetError());
   10.21  		exit(1);
   10.22  	}
   10.23 -	atexit(SDL_Quit);
   10.24 +	atexit(SDL_Quit_Wrapper);
   10.25  
   10.26  	if ( (mutex=SDL_CreateMutex()) == NULL ) {
   10.27  		fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
    11.1 --- a/test/testoverlay.c	Wed Sep 28 06:38:22 2005 +0000
    11.2 +++ b/test/testoverlay.c	Wed Sep 28 11:36:20 2005 +0000
    11.3 @@ -21,6 +21,13 @@
    11.4  int luminance;
    11.5  int w, h;
    11.6  
    11.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    11.8 +static void quit(int rc)
    11.9 +{
   11.10 +	SDL_Quit();
   11.11 +	exit(rc);
   11.12 +}
   11.13 +
   11.14  /* NOTE: These RGB conversion functions are not intended for speed,
   11.15           only as examples.
   11.16  */
   11.17 @@ -347,7 +354,7 @@
   11.18  			} else {
   11.19  				fprintf(stderr,
   11.20  				"The -delay option requires an argument\n");
   11.21 -				exit(1);
   11.22 +				return(1);
   11.23  			}
   11.24  		} else
   11.25  		if ( strcmp(argv[1], "-width") == 0 ) {
   11.26 @@ -357,7 +364,7 @@
   11.27  			} else {
   11.28  				fprintf(stderr,
   11.29  				"The -width option requires an argument\n");
   11.30 -				exit(1);
   11.31 +				return(1);
   11.32  			}
   11.33  		} else
   11.34  		if ( strcmp(argv[1], "-height") == 0 ) {
   11.35 @@ -367,7 +374,7 @@
   11.36  			} else {
   11.37  				fprintf(stderr,
   11.38  				"The -height option requires an argument\n");
   11.39 -				exit(1);
   11.40 +				return(1);
   11.41  			}
   11.42  		} else
   11.43  		if ( strcmp(argv[1], "-bpp") == 0 ) {
   11.44 @@ -378,7 +385,7 @@
   11.45  			} else {
   11.46  				fprintf(stderr,
   11.47  				"The -bpp option requires an argument\n");
   11.48 -				exit(1);
   11.49 +				return(1);
   11.50  			}
   11.51  		} else
   11.52  		if ( strcmp(argv[1], "-lum") == 0 ) {
   11.53 @@ -389,7 +396,7 @@
   11.54  			} else {
   11.55  				fprintf(stderr,
   11.56  				"The -lum option requires an argument\n");
   11.57 -				exit(1);
   11.58 +				return(1);
   11.59  			}
   11.60  		} else
   11.61  		if ( strcmp(argv[1], "-format") == 0 ) {
   11.62 @@ -407,14 +414,14 @@
   11.63  				else
   11.64  				{
   11.65  					fprintf(stderr, "The -format option %s is not recognized\n",argv[2]);
   11.66 -					exit(1);
   11.67 +					return(1);
   11.68  				}
   11.69  				argv += 2;
   11.70  				argc -= 2;
   11.71  			} else {
   11.72  				fprintf(stderr,
   11.73  				"The -format option requires an argument\n");
   11.74 -				exit(1);
   11.75 +				return(1);
   11.76  			}
   11.77  		} else
   11.78  		if ( strcmp(argv[1], "-hw") == 0 ) {
   11.79 @@ -439,7 +446,7 @@
   11.80  		} else
   11.81  		if (( strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) {
   11.82                          PrintUsage(argv0);
   11.83 -                        exit(1);
   11.84 +                        return(1);
   11.85  		} else
   11.86  		if ( strcmp(argv[1], "-fullscreen") == 0 ) {
   11.87  			video_flags |= SDL_FULLSCREEN;
   11.88 @@ -451,16 +458,15 @@
   11.89  	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   11.90  		fprintf(stderr,
   11.91  			"Couldn't initialize SDL: %s\n", SDL_GetError());
   11.92 -		exit(1);
   11.93 +		return(1);
   11.94  	}
   11.95 -	atexit(SDL_Quit);			/* Clean up on exit */
   11.96  
   11.97  	/* Initialize the display */
   11.98  	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
   11.99  	if ( screen == NULL ) {
  11.100  		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
  11.101  					w, h, desired_bpp, SDL_GetError());
  11.102 -		exit(1);
  11.103 +		quit(1);
  11.104  	}
  11.105  	printf("Set%s %dx%dx%d mode\n",
  11.106  			screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
  11.107 @@ -481,7 +487,7 @@
  11.108  	if ( pic == NULL ) {
  11.109  		fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
  11.110  							SDL_GetError());
  11.111 -		exit(1);
  11.112 +		quit(1);
  11.113  	}
  11.114  
  11.115  	/* Convert the picture to 32bits, for easy conversion */
  11.116 @@ -518,7 +524,7 @@
  11.117  		{
  11.118  			fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n",
  11.119  							SDL_GetError());
  11.120 -			exit(1);
  11.121 +			quit(1);
  11.122  		}
  11.123  		SDL_FreeSurface(pic);
  11.124  		pic=newsurf;
  11.125 @@ -528,7 +534,7 @@
  11.126  	overlay = SDL_CreateYUVOverlay(pic->w, pic->h, overlay_format, screen);
  11.127  	if ( overlay == NULL ) {
  11.128  		fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
  11.129 -		exit(1);
  11.130 +		quit(1);
  11.131  	}
  11.132  	printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
  11.133  			overlay->hw_overlay?"hardware":"software",
  11.134 @@ -566,7 +572,7 @@
  11.135  			break;
  11.136  		default:
  11.137  			printf("cannot convert RGB picture to obtained YUV format!\n");
  11.138 -			exit(1);
  11.139 +			quit(1);
  11.140  			break;
  11.141  	}
  11.142  #ifdef BENCHMARK_SDL
  11.143 @@ -584,6 +590,7 @@
  11.144  	printf("Time: %d milliseconds\n", now-then);
  11.145  #endif
  11.146  	SDL_Delay(delay*1000);
  11.147 +	SDL_Quit();
  11.148  	return(0);
  11.149  }
  11.150  
    12.1 --- a/test/testoverlay2.c	Wed Sep 28 06:38:22 2005 +0000
    12.2 +++ b/test/testoverlay2.c	Wed Sep 28 11:36:20 2005 +0000
    12.3 @@ -41,6 +41,14 @@
    12.4      {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
    12.5  };
    12.6  
    12.7 +
    12.8 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    12.9 +static void quit(int rc)
   12.10 +{
   12.11 +	SDL_Quit();
   12.12 +	exit(rc);
   12.13 +}
   12.14 +
   12.15  /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
   12.16  
   12.17  /* NOTE: These RGB conversion functions are not intended for speed,
   12.18 @@ -288,6 +296,12 @@
   12.19      int overlay_format=SDL_YUY2_OVERLAY;
   12.20      int scale=5;
   12.21  
   12.22 +    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
   12.23 +    {
   12.24 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   12.25 +        return 3;
   12.26 +    }
   12.27 +
   12.28      while ( argc > 1 )
   12.29      {
   12.30          if (strcmp(argv[1], "-fps")== 0)
   12.31 @@ -298,12 +312,12 @@
   12.32                  if (fps==0)
   12.33                  {
   12.34                      fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   12.35 -                    return -1;
   12.36 +                    quit(10);
   12.37                  }
   12.38                  if ((fps<0) || (fps>1000))
   12.39                  {
   12.40                      fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n");
   12.41 -                    return -1;
   12.42 +                    quit(10);
   12.43                  }
   12.44                  argv += 2;
   12.45                  argc -= 2;
   12.46 @@ -311,7 +325,7 @@
   12.47              else
   12.48              {
   12.49                  fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   12.50 -                return -1;
   12.51 +                quit(10);
   12.52              }
   12.53          } else
   12.54          if (strcmp(argv[1], "-format") == 0)
   12.55 @@ -331,7 +345,7 @@
   12.56                  else
   12.57                  {
   12.58                      fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]);
   12.59 -                    return -1;
   12.60 +                    quit(10);
   12.61                  }
   12.62                  argv += 2;
   12.63                  argc -= 2;
   12.64 @@ -339,7 +353,7 @@
   12.65              else
   12.66              {
   12.67                  fprintf(stderr, "The -format option requires an argument, default is YUY2.\n");
   12.68 -                return -1;
   12.69 +                quit(10);
   12.70              }
   12.71          } else
   12.72          if (strcmp(argv[1], "-scale") == 0)
   12.73 @@ -350,12 +364,12 @@
   12.74                  if (scale==0)
   12.75                  {
   12.76                      fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n");
   12.77 -                    return -1;
   12.78 +                    quit(10);
   12.79                  }
   12.80                  if ((scale<0) || (scale>50))
   12.81                  {
   12.82                      fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n");
   12.83 -                    return -1;
   12.84 +                    quit(10);
   12.85                  }
   12.86                  argv += 2;
   12.87                  argc -= 2;
   12.88 @@ -363,17 +377,17 @@
   12.89              else
   12.90              {
   12.91                  fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   12.92 -                return -1;
   12.93 +                quit(10);
   12.94              }
   12.95          } else
   12.96          if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0))
   12.97          {
   12.98              PrintUsage(argv[0]);
   12.99 -            return 0;
  12.100 +            quit(0);
  12.101          } else
  12.102          {
  12.103              fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
  12.104 -            return -1;
  12.105 +            quit(10);
  12.106          }
  12.107          break;
  12.108      }
  12.109 @@ -383,7 +397,7 @@
  12.110      {
  12.111          fprintf(stderr, "Can't allocate memory for movie !\n");
  12.112          free(RawMooseData);
  12.113 -        return 1;
  12.114 +        quit(1);
  12.115      }
  12.116  
  12.117      /* load the trojan moose images */
  12.118 @@ -392,27 +406,19 @@
  12.119      {
  12.120          fprintf(stderr, "Can't find the file moose.dat !\n");
  12.121          free(RawMooseData);
  12.122 -        return 2;
  12.123 +        quit(2);
  12.124      }
  12.125     
  12.126      SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
  12.127  
  12.128      SDL_RWclose(handle);
  12.129  
  12.130 -    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
  12.131 -    {
  12.132 -        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
  12.133 -        free(RawMooseData);
  12.134 -        return 3;
  12.135 -    }
  12.136 -    atexit(SDL_Quit);
  12.137 -
  12.138      /* Set video mode */
  12.139      if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL )
  12.140      {
  12.141          fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError());
  12.142          free(RawMooseData);
  12.143 -        return 4;
  12.144 +        quit(4);
  12.145      }
  12.146  
  12.147      /* Set the window manager title bar */
  12.148 @@ -426,7 +432,7 @@
  12.149          {
  12.150              fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError());
  12.151              free(RawMooseData);
  12.152 -            return 5;
  12.153 +            quit(5);
  12.154          }
  12.155          SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
  12.156  
  12.157 @@ -462,7 +468,7 @@
  12.158  		if(!newsurf)
  12.159  		{
  12.160                      fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError());
  12.161 -                    return 6;
  12.162 +                    quit(6);
  12.163  		}
  12.164  		SDL_FreeSurface(MooseFrame[i]);
  12.165  		MooseFrame[i]=newsurf;
  12.166 @@ -475,7 +481,7 @@
  12.167      if (!overlay)
  12.168      {
  12.169          fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
  12.170 -        return 7;
  12.171 +        quit(7);
  12.172      }
  12.173  
  12.174      printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
  12.175 @@ -538,7 +544,7 @@
  12.176                       {
  12.177                           SDL_FreeSurface(MooseFrame[i]);
  12.178                       }
  12.179 -                     return 0;
  12.180 +                     quit(0);
  12.181              }
  12.182          }
  12.183  
  12.184 @@ -586,6 +592,7 @@
  12.185          SDL_Delay(1);
  12.186      }
  12.187  
  12.188 +	SDL_Quit();
  12.189      return 0;
  12.190  }
  12.191  
    13.1 --- a/test/testpalette.c	Wed Sep 28 06:38:22 2005 +0000
    13.2 +++ b/test/testpalette.c	Wed Sep 28 11:36:20 2005 +0000
    13.3 @@ -54,10 +54,17 @@
    13.4      {0,39,172}, {0,28,152}, {0,17,132}, {0,7,114}
    13.5  };
    13.6  
    13.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    13.8 +static void quit(int rc)
    13.9 +{
   13.10 +	SDL_Quit();
   13.11 +	exit(rc);
   13.12 +}
   13.13 +
   13.14  static void sdlerr(char *when)
   13.15  {
   13.16      fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
   13.17 -    exit(1);
   13.18 +    quit(1);
   13.19  }
   13.20  
   13.21  /* create a background surface */
   13.22 @@ -139,8 +146,6 @@
   13.23      if(SDL_Init(SDL_INIT_VIDEO) < 0)
   13.24  	sdlerr("initialising SDL");
   13.25  
   13.26 -    atexit(SDL_Quit);
   13.27 -
   13.28      while(--argc) {
   13.29  	++argv;
   13.30  	if(strcmp(*argv, "-hw") == 0)
   13.31 @@ -157,7 +162,7 @@
   13.32  	    fprintf(stderr,
   13.33  		    "usage: testpalette "
   13.34  		    " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
   13.35 -	    return 1;
   13.36 +	    quit(1);
   13.37  	}
   13.38      }
   13.39  
   13.40 @@ -165,7 +170,7 @@
   13.41      if(!(screen = SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE))) {
   13.42  	fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
   13.43  		SCRW, SCRH, SDL_GetError());
   13.44 -	return 1;
   13.45 +	quit(1);
   13.46      }
   13.47  
   13.48      if(!(boat[0] = SDL_LoadBMP("sail.bmp")))
   13.49 @@ -327,6 +332,8 @@
   13.50  
   13.51      printf("%d frames, %.2f fps\n",
   13.52  	   frames, 1000.0 * frames / (SDL_GetTicks() - start));
   13.53 +
   13.54 +    SDL_Quit();
   13.55      return 0;
   13.56  }
   13.57  
    14.1 --- a/test/testsem.c	Wed Sep 28 06:38:22 2005 +0000
    14.2 +++ b/test/testsem.c	Wed Sep 28 11:36:20 2005 +0000
    14.3 @@ -13,6 +13,13 @@
    14.4  static SDL_sem *sem;
    14.5  int alive = 1;
    14.6  
    14.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    14.8 +static void quit(int rc)
    14.9 +{
   14.10 +	SDL_Quit();
   14.11 +	exit(rc);
   14.12 +}
   14.13 +
   14.14  int ThreadFunc(void *data)
   14.15  {
   14.16  	while ( alive ) {
   14.17 @@ -39,15 +46,14 @@
   14.18  
   14.19  	if(argc < 2) {
   14.20  		fprintf(stderr,"Usage: %s init_value\n", argv[0]);
   14.21 -		exit(1);
   14.22 +		return(1);
   14.23  	}
   14.24  
   14.25  	/* Load the SDL library */
   14.26  	if ( SDL_Init(0) < 0 ) {
   14.27  		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   14.28 -		exit(1);
   14.29 +		return(1);
   14.30  	}
   14.31 -	atexit(SDL_Quit);
   14.32  	signal(SIGTERM, killed);
   14.33  	signal(SIGINT, killed);
   14.34  	
   14.35 @@ -72,5 +78,6 @@
   14.36  	printf("Finished waiting for threads\n");
   14.37  
   14.38  	SDL_DestroySemaphore(sem);
   14.39 +	SDL_Quit();
   14.40  	return(0);
   14.41  }
    15.1 --- a/test/testsprite.c	Wed Sep 28 06:38:22 2005 +0000
    15.2 +++ b/test/testsprite.c	Wed Sep 28 11:36:20 2005 +0000
    15.3 @@ -22,6 +22,13 @@
    15.4  int sprites_visible;
    15.5  Uint16 sprite_w, sprite_h;
    15.6  
    15.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    15.8 +static void quit(int rc)
    15.9 +{
   15.10 +	SDL_Quit();
   15.11 +	exit(rc);
   15.12 +}
   15.13 +
   15.14  int LoadSprite(SDL_Surface *screen, char *file)
   15.15  {
   15.16  	SDL_Surface *temp;
   15.17 @@ -159,9 +166,8 @@
   15.18  	/* Initialize SDL */
   15.19  	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   15.20  		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   15.21 -		exit(1);
   15.22 +		return(1);
   15.23  	}
   15.24 -	atexit(SDL_Quit);
   15.25  
   15.26  	numsprites = NUM_SPRITES;
   15.27  	videoflags = SDL_SWSURFACE|SDL_ANYFORMAT;
   15.28 @@ -201,7 +207,7 @@
   15.29  			fprintf(stderr, 
   15.30  	"Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
   15.31  								argv[0]);
   15.32 -			exit(1);
   15.33 +			quit(1);
   15.34  		}
   15.35  	}
   15.36  
   15.37 @@ -210,12 +216,12 @@
   15.38  	if ( ! screen ) {
   15.39  		fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
   15.40  					width, height, SDL_GetError());
   15.41 -		exit(2);
   15.42 +		quit(2);
   15.43  	}
   15.44  
   15.45  	/* Load the sprite */
   15.46  	if ( LoadSprite(screen, "icon.bmp") < 0 ) {
   15.47 -		exit(1);
   15.48 +		quit(1);
   15.49  	}
   15.50  
   15.51  	/* Allocate memory for the sprite info */
   15.52 @@ -223,7 +229,7 @@
   15.53  	if ( mem == NULL ) {
   15.54  		SDL_FreeSurface(sprite);
   15.55  		fprintf(stderr, "Out of memory!\n");
   15.56 -		exit(2);
   15.57 +		quit(2);
   15.58  	}
   15.59  	sprite_rects = (SDL_Rect *)mem;
   15.60  	positions = sprite_rects;
    16.1 --- a/test/testtimer.c	Wed Sep 28 06:38:22 2005 +0000
    16.2 +++ b/test/testtimer.c	Wed Sep 28 11:36:20 2005 +0000
    16.3 @@ -12,6 +12,13 @@
    16.4  
    16.5  static int ticks = 0;
    16.6  
    16.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    16.8 +static void quit(int rc)
    16.9 +{
   16.10 +	SDL_Quit();
   16.11 +	exit(rc);
   16.12 +}
   16.13 +
   16.14  static Uint32 ticktock(Uint32 interval)
   16.15  {
   16.16  	++ticks;
   16.17 @@ -31,9 +38,8 @@
   16.18  
   16.19  	if ( SDL_Init(SDL_INIT_TIMER) < 0 ) {
   16.20  		fprintf(stderr, "Couldn't load SDL: %s\n", SDL_GetError());
   16.21 -		exit(1);
   16.22 +		return(1);
   16.23  	}
   16.24 -	atexit(SDL_Quit);
   16.25  
   16.26  	/* Start the timer */
   16.27  	desired = 0;
   16.28 @@ -83,5 +89,6 @@
   16.29  	SDL_RemoveTimer(t2);
   16.30  	SDL_RemoveTimer(t3);
   16.31  
   16.32 +	SDL_Quit();
   16.33  	return(0);
   16.34  }
    17.1 --- a/test/testwin.c	Wed Sep 28 06:38:22 2005 +0000
    17.2 +++ b/test/testwin.c	Wed Sep 28 11:36:20 2005 +0000
    17.3 @@ -11,6 +11,13 @@
    17.4  
    17.5  #include "SDL.h"
    17.6  
    17.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    17.8 +static void quit(int rc)
    17.9 +{
   17.10 +	SDL_Quit();
   17.11 +	exit(rc);
   17.12 +}
   17.13 +
   17.14  void DrawPict(SDL_Surface *screen, char *bmpfile,
   17.15  					int speedy, int flip, int nofade)
   17.16  {
   17.17 @@ -245,6 +252,13 @@
   17.18  	h = 480;
   17.19  	desired_bpp = 0;
   17.20  	video_flags = 0;
   17.21 +
   17.22 +	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   17.23 +		fprintf(stderr,
   17.24 +			"Couldn't initialize SDL: %s\n", SDL_GetError());
   17.25 +		return(1);
   17.26 +	}
   17.27 +
   17.28  	while ( argc > 1 ) {
   17.29  		if ( strcmp(argv[1], "-speedy") == 0 ) {
   17.30  			speedy = 1;
   17.31 @@ -264,7 +278,7 @@
   17.32  			} else {
   17.33  				fprintf(stderr,
   17.34  				"The -delay option requires an argument\n");
   17.35 -				exit(1);
   17.36 +				quit(1);
   17.37  			}
   17.38  		} else
   17.39  		if ( strcmp(argv[1], "-width") == 0 ) {
   17.40 @@ -274,7 +288,7 @@
   17.41  			} else {
   17.42  				fprintf(stderr,
   17.43  				"The -width option requires an argument\n");
   17.44 -				exit(1);
   17.45 +				quit(1);
   17.46  			}
   17.47  		} else
   17.48  		if ( strcmp(argv[1], "-height") == 0 ) {
   17.49 @@ -284,7 +298,7 @@
   17.50  			} else {
   17.51  				fprintf(stderr,
   17.52  				"The -height option requires an argument\n");
   17.53 -				exit(1);
   17.54 +				quit(1);
   17.55  			}
   17.56  		} else
   17.57  		if ( strcmp(argv[1], "-bpp") == 0 ) {
   17.58 @@ -295,7 +309,7 @@
   17.59  			} else {
   17.60  				fprintf(stderr,
   17.61  				"The -bpp option requires an argument\n");
   17.62 -				exit(1);
   17.63 +				quit(1);
   17.64  			}
   17.65  		} else
   17.66  		if ( strcmp(argv[1], "-warp") == 0 ) {
   17.67 @@ -321,19 +335,12 @@
   17.68  			break;
   17.69  	}
   17.70  
   17.71 -	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   17.72 -		fprintf(stderr,
   17.73 -			"Couldn't initialize SDL: %s\n", SDL_GetError());
   17.74 -		exit(1);
   17.75 -	}
   17.76 -	atexit(SDL_Quit);			/* Clean up on exit */
   17.77 -
   17.78  	/* Initialize the display */
   17.79  	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
   17.80  	if ( screen == NULL ) {
   17.81  		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
   17.82  					w, h, desired_bpp, SDL_GetError());
   17.83 -		exit(1);
   17.84 +		quit(1);
   17.85  	}
   17.86  	printf("Set%s %dx%dx%d mode\n",
   17.87  			screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
   17.88 @@ -358,5 +365,6 @@
   17.89  	DrawPict(screen, argv[1], speedy, flip, nofade);
   17.90  #endif
   17.91  	SDL_Delay(delay*1000);
   17.92 +	SDL_Quit();
   17.93  	return(0);
   17.94  }
    18.1 --- a/test/testwm.c	Wed Sep 28 06:38:22 2005 +0000
    18.2 +++ b/test/testwm.c	Wed Sep 28 11:36:20 2005 +0000
    18.3 @@ -13,6 +13,13 @@
    18.4  static Uint8  video_bpp;
    18.5  static Uint32 video_flags;
    18.6  
    18.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    18.8 +static void quit(int rc)
    18.9 +{
   18.10 +	SDL_Quit();
   18.11 +	exit(rc);
   18.12 +}
   18.13 +
   18.14  int SetVideoMode(int w, int h)
   18.15  {
   18.16  	SDL_Surface *screen;
   18.17 @@ -264,9 +271,8 @@
   18.18  	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   18.19  		fprintf(stderr,
   18.20  			"Couldn't initialize SDL: %s\n", SDL_GetError());
   18.21 -		exit(1);
   18.22 +		return(1);
   18.23  	}
   18.24 -	atexit(SDL_Quit);
   18.25  
   18.26  	/* Check command line arguments */
   18.27  	w = 640;
   18.28 @@ -333,7 +339,7 @@
   18.29  
   18.30  	/* Initialize the display */
   18.31  	if ( SetVideoMode(w, h) < 0 ) {
   18.32 -		return(1);
   18.33 +		quit(1);
   18.34  	}
   18.35  
   18.36  	/* Set an event filter that discards everything but QUIT */
   18.37 @@ -355,7 +361,7 @@
   18.38  				/* Fall through to the quit handler */
   18.39  			case SDL_QUIT:
   18.40  				printf("Bye bye..\n");
   18.41 -				return(0);
   18.42 +				quit(0);
   18.43  			default:
   18.44  				/* This should never happen */
   18.45  				printf("Warning: Event %d wasn't filtered\n",
   18.46 @@ -364,5 +370,6 @@
   18.47  		}
   18.48  	}
   18.49  	printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
   18.50 +	SDL_Quit();
   18.51  	return(255);
   18.52  }
    19.1 --- a/test/threadwin.c	Wed Sep 28 06:38:22 2005 +0000
    19.2 +++ b/test/threadwin.c	Wed Sep 28 11:36:20 2005 +0000
    19.3 @@ -14,6 +14,13 @@
    19.4  /* Is the cursor visible? */
    19.5  static int visible = 1;
    19.6  
    19.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    19.8 +static void quit(int rc)
    19.9 +{
   19.10 +	SDL_Quit();
   19.11 +	exit(rc);
   19.12 +}
   19.13 +
   19.14  SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp)
   19.15  {
   19.16  	SDL_Surface *icon;
   19.17 @@ -260,9 +267,8 @@
   19.18  	if ( SDL_Init(init_flags) < 0 ) {
   19.19  		fprintf(stderr,
   19.20  			"Couldn't initialize SDL: %s\n", SDL_GetError());
   19.21 -		exit(1);
   19.22 +		return(1);
   19.23  	}
   19.24 -	atexit(SDL_Quit);
   19.25  
   19.26  	/* Set the icon -- this must be done before the first mode set */
   19.27  	icon = LoadIconSurface("icon.bmp", &icon_mask);
   19.28 @@ -277,7 +283,7 @@
   19.29  	if (  screen == NULL ) {
   19.30  		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
   19.31  						video_bpp, SDL_GetError());
   19.32 -		exit(1);
   19.33 +		quit(1);
   19.34  	}
   19.35  	printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
   19.36  						"fullscreen" : "windowed");
   19.37 @@ -302,7 +308,7 @@
   19.38  	if ( SDL_LockSurface(screen) < 0 ) {
   19.39  		fprintf(stderr, "Couldn't lock display surface: %s\n",
   19.40  							SDL_GetError());
   19.41 -		exit(2);
   19.42 +		quit(2);
   19.43  	}
   19.44  	buffer = (Uint8 *)screen->pixels;
   19.45  	for ( i=0; i<screen->h; ++i ) {
   19.46 @@ -326,5 +332,6 @@
   19.47  	}
   19.48  	SDL_WaitThread(mouse_thread, NULL);
   19.49  	SDL_WaitThread(keybd_thread, NULL);
   19.50 +	SDL_Quit();
   19.51  	return(0);
   19.52  }
    20.1 --- a/test/torturethread.c	Wed Sep 28 06:38:22 2005 +0000
    20.2 +++ b/test/torturethread.c	Wed Sep 28 11:36:20 2005 +0000
    20.3 @@ -13,6 +13,13 @@
    20.4  
    20.5  static char volatile time_for_threads_to_die[NUMTHREADS];
    20.6  
    20.7 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    20.8 +static void quit(int rc)
    20.9 +{
   20.10 +	SDL_Quit();
   20.11 +	exit(rc);
   20.12 +}
   20.13 +
   20.14  int SubThreadFunc(void *data) {
   20.15  	while(! *(int volatile *)data) {
   20.16  		; /*SDL_Delay(10); /* do nothing */
   20.17 @@ -57,10 +64,8 @@
   20.18  	/* Load the SDL library */
   20.19  	if ( SDL_Init(0) < 0 ) {
   20.20  		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   20.21 -		exit(1);
   20.22 +		return(1);
   20.23  	}
   20.24 -	atexit(SDL_Quit);
   20.25 -
   20.26  
   20.27  	signal(SIGSEGV, SIG_DFL);
   20.28  	for(i = 0; i < NUMTHREADS; i++) {
   20.29 @@ -70,7 +75,7 @@
   20.30  		if ( threads[i] == NULL ) {
   20.31  			fprintf(stderr,
   20.32  			"Couldn't create thread: %s\n", SDL_GetError());
   20.33 -			exit(1);
   20.34 +			quit(1);
   20.35  		}
   20.36  	}
   20.37  
   20.38 @@ -81,5 +86,6 @@
   20.39  	for(i = NUMTHREADS-1; i >= 0; --i) {
   20.40  		SDL_WaitThread(threads[i], NULL);
   20.41  	}
   20.42 -	return(0);	/* Never reached */
   20.43 +	SDL_Quit();
   20.44 +	return(0);
   20.45  }