From b77cfd625af1620704c3415c01684a95b430688c Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 28 Sep 2005 11:36:20 +0000 Subject: [PATCH] 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 | 16 +++++++++---- test/loopwave.c | 19 +++++++++++---- test/testalpha.c | 23 ++++++++++++------ test/testbitmap.c | 19 ++++++++++----- test/testcdrom.c | 16 +++++++++---- test/testdyngl.c | 19 +++++++++------ test/testerror.c | 13 +++++++--- test/testgamma.c | 15 ++++++++---- test/testhread.c | 19 ++++++++++----- test/testlock.c | 11 ++++++++- test/testoverlay.c | 37 ++++++++++++++++------------ test/testoverlay2.c | 57 +++++++++++++++++++++++++------------------- test/testpalette.c | 17 +++++++++---- test/testsem.c | 13 +++++++--- test/testsprite.c | 18 +++++++++----- test/testtimer.c | 11 +++++++-- test/testwin.c | 32 +++++++++++++++---------- test/testwm.c | 15 ++++++++---- test/threadwin.c | 15 ++++++++---- test/torturethread.c | 16 +++++++++---- 20 files changed, 272 insertions(+), 129 deletions(-) diff --git a/test/checkkeys.c b/test/checkkeys.c index 9901bf032..a458ec314 100644 --- a/test/checkkeys.c +++ b/test/checkkeys.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,7 +99,7 @@ int main(int argc, char *argv[]) videoflags |= SDL_FULLSCREEN; } else { fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]); - exit(1); + quit(1); } } @@ -101,7 +107,7 @@ int main(int argc, char *argv[]) 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); } diff --git a/test/loopwave.c b/test/loopwave.c index ea767f041..94ba906e3 100644 --- a/test/loopwave.c +++ b/test/loopwave.c @@ -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,13 +63,12 @@ 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 \n", argv[0]); - exit(1); + quit(1); } /* Load the wave file into memory */ @@ -68,7 +76,7 @@ int main(int argc, char *argv[]) &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); } diff --git a/test/testalpha.c b/test/testalpha.c index d0a0b44ff..d817e44b7 100644 --- a/test/testalpha.c +++ b/test/testalpha.c @@ -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,7 +334,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", argv[0]); - exit(1); + quit(1); } } @@ -335,14 +342,14 @@ int main(int argc, char *argv[]) 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); } diff --git a/test/testbitmap.c b/test/testbitmap.c index 971b298f8..50cfcbb22 100644 --- a/test/testbitmap.c +++ b/test/testbitmap.c @@ -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,7 +91,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", argv[0]); - exit(1); + quit(1); } } @@ -93,7 +99,7 @@ int main(int argc, char *argv[]) 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); } diff --git a/test/testcdrom.c b/test/testcdrom.c index 2ace1b8a1..8f9a50b1b 100644 --- a/test/testcdrom.c +++ b/test/testcdrom.c @@ -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; iflags & SDL_FULLSCREEN ? " fullscreen" : "", @@ -481,7 +487,7 @@ int main(int argc, char **argv) if ( pic == NULL ) { fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError()); - exit(1); + quit(1); } /* Convert the picture to 32bits, for easy conversion */ @@ -518,7 +524,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError()); - exit(1); + quit(1); } SDL_FreeSurface(pic); pic=newsurf; @@ -528,7 +534,7 @@ int main(int argc, char **argv) overlay = SDL_CreateYUVOverlay(pic->w, pic->h, overlay_format, screen); if ( overlay == NULL ) { fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); - exit(1); + quit(1); } printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes, overlay->hw_overlay?"hardware":"software", @@ -566,7 +572,7 @@ int main(int argc, char **argv) break; default: printf("cannot convert RGB picture to obtained YUV format!\n"); - exit(1); + quit(1); break; } #ifdef BENCHMARK_SDL @@ -584,6 +590,7 @@ int main(int argc, char **argv) printf("Time: %d milliseconds\n", now-then); #endif SDL_Delay(delay*1000); + SDL_Quit(); return(0); } diff --git a/test/testoverlay2.c b/test/testoverlay2.c index 5ac1629b6..84594cac9 100644 --- a/test/testoverlay2.c +++ b/test/testoverlay2.c @@ -41,6 +41,14 @@ SDL_Color MooseColors[84]={ {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173} }; + +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ +static void quit(int rc) +{ + SDL_Quit(); + exit(rc); +} + /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */ /* NOTE: These RGB conversion functions are not intended for speed, @@ -288,6 +296,12 @@ int main(int argc, char **argv) int overlay_format=SDL_YUY2_OVERLAY; int scale=5; + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) + { + fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); + return 3; + } + while ( argc > 1 ) { if (strcmp(argv[1], "-fps")== 0) @@ -298,12 +312,12 @@ int main(int argc, char **argv) if (fps==0) { fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); - return -1; + quit(10); } if ((fps<0) || (fps>1000)) { fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n"); - return -1; + quit(10); } argv += 2; argc -= 2; @@ -311,7 +325,7 @@ int main(int argc, char **argv) else { fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); - return -1; + quit(10); } } else if (strcmp(argv[1], "-format") == 0) @@ -331,7 +345,7 @@ int main(int argc, char **argv) else { fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]); - return -1; + quit(10); } argv += 2; argc -= 2; @@ -339,7 +353,7 @@ int main(int argc, char **argv) else { fprintf(stderr, "The -format option requires an argument, default is YUY2.\n"); - return -1; + quit(10); } } else if (strcmp(argv[1], "-scale") == 0) @@ -350,12 +364,12 @@ int main(int argc, char **argv) if (scale==0) { fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n"); - return -1; + quit(10); } if ((scale<0) || (scale>50)) { fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n"); - return -1; + quit(10); } argv += 2; argc -= 2; @@ -363,17 +377,17 @@ int main(int argc, char **argv) else { fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); - return -1; + quit(10); } } else if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) { PrintUsage(argv[0]); - return 0; + quit(0); } else { fprintf(stderr, "Unrecognized option: %s.\n", argv[1]); - return -1; + quit(10); } break; } @@ -383,7 +397,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Can't allocate memory for movie !\n"); free(RawMooseData); - return 1; + quit(1); } /* load the trojan moose images */ @@ -392,27 +406,19 @@ int main(int argc, char **argv) { fprintf(stderr, "Can't find the file moose.dat !\n"); free(RawMooseData); - return 2; + quit(2); } SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT); SDL_RWclose(handle); - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) - { - fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); - free(RawMooseData); - return 3; - } - atexit(SDL_Quit); - /* Set video mode */ if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL ) { fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError()); free(RawMooseData); - return 4; + quit(4); } /* Set the window manager title bar */ @@ -426,7 +432,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError()); free(RawMooseData); - return 5; + quit(5); } SDL_SetColors(MooseFrame[i], MooseColors, 0, 84); @@ -462,7 +468,7 @@ int main(int argc, char **argv) if(!newsurf) { fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError()); - return 6; + quit(6); } SDL_FreeSurface(MooseFrame[i]); MooseFrame[i]=newsurf; @@ -475,7 +481,7 @@ int main(int argc, char **argv) if (!overlay) { fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); - return 7; + quit(7); } printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes, @@ -538,7 +544,7 @@ int main(int argc, char **argv) { SDL_FreeSurface(MooseFrame[i]); } - return 0; + quit(0); } } @@ -586,6 +592,7 @@ int main(int argc, char **argv) SDL_Delay(1); } + SDL_Quit(); return 0; } diff --git a/test/testpalette.c b/test/testpalette.c index c1b32765b..9436a9b35 100644 --- a/test/testpalette.c +++ b/test/testpalette.c @@ -54,10 +54,17 @@ static SDL_Color wavemap[] = { {0,39,172}, {0,28,152}, {0,17,132}, {0,7,114} }; +/* 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 sdlerr(char *when) { fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError()); - exit(1); + quit(1); } /* create a background surface */ @@ -139,8 +146,6 @@ int main(int argc, char **argv) if(SDL_Init(SDL_INIT_VIDEO) < 0) sdlerr("initialising SDL"); - atexit(SDL_Quit); - while(--argc) { ++argv; if(strcmp(*argv, "-hw") == 0) @@ -157,7 +162,7 @@ int main(int argc, char **argv) fprintf(stderr, "usage: testpalette " " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n"); - return 1; + quit(1); } } @@ -165,7 +170,7 @@ int main(int argc, char **argv) if(!(screen = SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE))) { fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n", SCRW, SCRH, SDL_GetError()); - return 1; + quit(1); } if(!(boat[0] = SDL_LoadBMP("sail.bmp"))) @@ -327,6 +332,8 @@ int main(int argc, char **argv) printf("%d frames, %.2f fps\n", frames, 1000.0 * frames / (SDL_GetTicks() - start)); + + SDL_Quit(); return 0; } diff --git a/test/testsem.c b/test/testsem.c index 65d8b7573..bbcf11711 100644 --- a/test/testsem.c +++ b/test/testsem.c @@ -13,6 +13,13 @@ static SDL_sem *sem; int alive = 1; +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ +static void quit(int rc) +{ + SDL_Quit(); + exit(rc); +} + int ThreadFunc(void *data) { while ( alive ) { @@ -39,15 +46,14 @@ int main(int argc, char **argv) if(argc < 2) { fprintf(stderr,"Usage: %s init_value\n", argv[0]); - exit(1); + return(1); } /* Load the SDL library */ if ( SDL_Init(0) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); - exit(1); + return(1); } - atexit(SDL_Quit); signal(SIGTERM, killed); signal(SIGINT, killed); @@ -72,5 +78,6 @@ int main(int argc, char **argv) printf("Finished waiting for threads\n"); SDL_DestroySemaphore(sem); + SDL_Quit(); return(0); } diff --git a/test/testsprite.c b/test/testsprite.c index cd2ba4180..3445361b6 100644 --- a/test/testsprite.c +++ b/test/testsprite.c @@ -22,6 +22,13 @@ SDL_Rect *velocities; int sprites_visible; Uint16 sprite_w, sprite_h; +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ +static void quit(int rc) +{ + SDL_Quit(); + exit(rc); +} + int LoadSprite(SDL_Surface *screen, char *file) { SDL_Surface *temp; @@ -159,9 +166,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); numsprites = NUM_SPRITES; videoflags = SDL_SWSURFACE|SDL_ANYFORMAT; @@ -201,7 +207,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n", argv[0]); - exit(1); + quit(1); } } @@ -210,12 +216,12 @@ int main(int argc, char *argv[]) if ( ! screen ) { fprintf(stderr, "Couldn't set %dx%d video mode: %s\n", width, height, SDL_GetError()); - exit(2); + quit(2); } /* Load the sprite */ if ( LoadSprite(screen, "icon.bmp") < 0 ) { - exit(1); + quit(1); } /* Allocate memory for the sprite info */ @@ -223,7 +229,7 @@ int main(int argc, char *argv[]) if ( mem == NULL ) { SDL_FreeSurface(sprite); fprintf(stderr, "Out of memory!\n"); - exit(2); + quit(2); } sprite_rects = (SDL_Rect *)mem; positions = sprite_rects; diff --git a/test/testtimer.c b/test/testtimer.c index c89cfee90..02b533cbb 100644 --- a/test/testtimer.c +++ b/test/testtimer.c @@ -12,6 +12,13 @@ static int ticks = 0; +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ +static void quit(int rc) +{ + SDL_Quit(); + exit(rc); +} + static Uint32 ticktock(Uint32 interval) { ++ticks; @@ -31,9 +38,8 @@ int main(int argc, char *argv[]) if ( SDL_Init(SDL_INIT_TIMER) < 0 ) { fprintf(stderr, "Couldn't load SDL: %s\n", SDL_GetError()); - exit(1); + return(1); } - atexit(SDL_Quit); /* Start the timer */ desired = 0; @@ -83,5 +89,6 @@ int main(int argc, char *argv[]) SDL_RemoveTimer(t2); SDL_RemoveTimer(t3); + SDL_Quit(); return(0); } diff --git a/test/testwin.c b/test/testwin.c index 94d1989e7..523573bf3 100644 --- a/test/testwin.c +++ b/test/testwin.c @@ -11,6 +11,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); +} + void DrawPict(SDL_Surface *screen, char *bmpfile, int speedy, int flip, int nofade) { @@ -245,6 +252,13 @@ int main(int argc, char *argv[]) h = 480; desired_bpp = 0; video_flags = 0; + + if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { + fprintf(stderr, + "Couldn't initialize SDL: %s\n", SDL_GetError()); + return(1); + } + while ( argc > 1 ) { if ( strcmp(argv[1], "-speedy") == 0 ) { speedy = 1; @@ -264,7 +278,7 @@ int main(int argc, char *argv[]) } else { fprintf(stderr, "The -delay option requires an argument\n"); - exit(1); + quit(1); } } else if ( strcmp(argv[1], "-width") == 0 ) { @@ -274,7 +288,7 @@ int main(int argc, char *argv[]) } else { fprintf(stderr, "The -width option requires an argument\n"); - exit(1); + quit(1); } } else if ( strcmp(argv[1], "-height") == 0 ) { @@ -284,7 +298,7 @@ int main(int argc, char *argv[]) } else { fprintf(stderr, "The -height option requires an argument\n"); - exit(1); + quit(1); } } else if ( strcmp(argv[1], "-bpp") == 0 ) { @@ -295,7 +309,7 @@ int main(int argc, char *argv[]) } else { fprintf(stderr, "The -bpp option requires an argument\n"); - exit(1); + quit(1); } } else if ( strcmp(argv[1], "-warp") == 0 ) { @@ -321,19 +335,12 @@ int main(int argc, char *argv[]) break; } - if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { - fprintf(stderr, - "Couldn't initialize SDL: %s\n", SDL_GetError()); - exit(1); - } - atexit(SDL_Quit); /* Clean up on exit */ - /* Initialize the display */ screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags); if ( screen == NULL ) { fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", w, h, desired_bpp, SDL_GetError()); - exit(1); + quit(1); } printf("Set%s %dx%dx%d mode\n", screen->flags & SDL_FULLSCREEN ? " fullscreen" : "", @@ -358,5 +365,6 @@ int main(int argc, char *argv[]) DrawPict(screen, argv[1], speedy, flip, nofade); #endif SDL_Delay(delay*1000); + SDL_Quit(); return(0); } diff --git a/test/testwm.c b/test/testwm.c index 4be8aca33..9783dbcfe 100644 --- a/test/testwm.c +++ b/test/testwm.c @@ -13,6 +13,13 @@ static int visible = 1; static Uint8 video_bpp; static Uint32 video_flags; +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ +static void quit(int rc) +{ + SDL_Quit(); + exit(rc); +} + int SetVideoMode(int w, int h) { SDL_Surface *screen; @@ -264,9 +271,8 @@ int main(int argc, char *argv[]) if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); - exit(1); + return(1); } - atexit(SDL_Quit); /* Check command line arguments */ w = 640; @@ -333,7 +339,7 @@ int main(int argc, char *argv[]) /* Initialize the display */ if ( SetVideoMode(w, h) < 0 ) { - return(1); + quit(1); } /* Set an event filter that discards everything but QUIT */ @@ -355,7 +361,7 @@ int main(int argc, char *argv[]) /* Fall through to the quit handler */ case SDL_QUIT: printf("Bye bye..\n"); - return(0); + quit(0); default: /* This should never happen */ printf("Warning: Event %d wasn't filtered\n", @@ -364,5 +370,6 @@ int main(int argc, char *argv[]) } } printf("SDL_WaitEvent() error: %s\n", SDL_GetError()); + SDL_Quit(); return(255); } diff --git a/test/threadwin.c b/test/threadwin.c index c1e8f38c2..20cd1bc91 100644 --- a/test/threadwin.c +++ b/test/threadwin.c @@ -14,6 +14,13 @@ static int done = 0; /* Is the cursor visible? */ static int visible = 1; +/* 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 *LoadIconSurface(char *file, Uint8 **maskp) { SDL_Surface *icon; @@ -260,9 +267,8 @@ int main(int argc, char *argv[]) if ( SDL_Init(init_flags) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); - exit(1); + return(1); } - atexit(SDL_Quit); /* Set the icon -- this must be done before the first mode set */ icon = LoadIconSurface("icon.bmp", &icon_mask); @@ -277,7 +283,7 @@ int main(int argc, char *argv[]) if ( screen == NULL ) { fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", video_bpp, SDL_GetError()); - exit(1); + quit(1); } printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ? "fullscreen" : "windowed"); @@ -302,7 +308,7 @@ int main(int argc, char *argv[]) if ( SDL_LockSurface(screen) < 0 ) { fprintf(stderr, "Couldn't lock display surface: %s\n", SDL_GetError()); - exit(2); + quit(2); } buffer = (Uint8 *)screen->pixels; for ( i=0; ih; ++i ) { @@ -326,5 +332,6 @@ int main(int argc, char *argv[]) } SDL_WaitThread(mouse_thread, NULL); SDL_WaitThread(keybd_thread, NULL); + SDL_Quit(); return(0); } diff --git a/test/torturethread.c b/test/torturethread.c index 50e38cc16..444e8d1e5 100644 --- a/test/torturethread.c +++ b/test/torturethread.c @@ -13,6 +13,13 @@ static char volatile time_for_threads_to_die[NUMTHREADS]; +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ +static void quit(int rc) +{ + SDL_Quit(); + exit(rc); +} + int SubThreadFunc(void *data) { while(! *(int volatile *)data) { ; /*SDL_Delay(10); /* do nothing */ @@ -57,10 +64,8 @@ int main(int argc, char *argv[]) /* Load the SDL library */ if ( SDL_Init(0) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); - exit(1); + return(1); } - atexit(SDL_Quit); - signal(SIGSEGV, SIG_DFL); for(i = 0; i < NUMTHREADS; i++) { @@ -70,7 +75,7 @@ int main(int argc, char *argv[]) if ( threads[i] == NULL ) { fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); - exit(1); + quit(1); } } @@ -81,5 +86,6 @@ int main(int argc, char *argv[]) for(i = NUMTHREADS-1; i >= 0; --i) { SDL_WaitThread(threads[i], NULL); } - return(0); /* Never reached */ + SDL_Quit(); + return(0); }