diff --git a/sc2/src/libs/graphics/dcqueue.c b/sc2/src/libs/graphics/dcqueue.c index f57891646..086cc1a4c 100644 --- a/sc2/src/libs/graphics/dcqueue.c +++ b/sc2/src/libs/graphics/dcqueue.c @@ -156,8 +156,17 @@ Init_DrawCommandQueue (void) void Uninit_DrawCommandQueue (void) { - DestroyCondVar (RenderingCond); - DestroyRecursiveMutex (DCQ_Mutex); + if (RenderingCond) + { + DestroyCondVar (RenderingCond); + RenderingCond = 0; + } + + if (DCQ_Mutex) + { + DestroyRecursiveMutex (DCQ_Mutex); + DCQ_Mutex = 0; + } } void diff --git a/sc2/src/libs/graphics/sdl/opengl.c b/sc2/src/libs/graphics/sdl/opengl.c index be8b68832..9597d38c5 100644 --- a/sc2/src/libs/graphics/sdl/opengl.c +++ b/sc2/src/libs/graphics/sdl/opengl.c @@ -274,6 +274,15 @@ TFB_GL_InitGraphics (int driver, int flags, int width, int height) return 0; } +void +TFB_GL_UninitGraphics (void) +{ + int i; + + for (i = 0; i < TFB_GFX_NUMSCREENS; i++) + UnInit_Screen (&GL_Screens[i].scaled); +} + void TFB_GL_UploadTransitionScreen (void) { GL_Screens[TFB_SCREEN_TRANSITION].updated.x = 0; diff --git a/sc2/src/libs/graphics/sdl/opengl.h b/sc2/src/libs/graphics/sdl/opengl.h index f5094462b..93af36cc1 100644 --- a/sc2/src/libs/graphics/sdl/opengl.h +++ b/sc2/src/libs/graphics/sdl/opengl.h @@ -22,6 +22,7 @@ #include "libs/graphics/sdl/sdl_common.h" int TFB_GL_InitGraphics (int driver, int flags, int width, int height); +void TFB_GL_UninitGraphics (void); int TFB_GL_ConfigureVideo (int driver, int flags, int width, int height, int togglefullscreen); void TFB_GL_UploadTransitionScreen (void); diff --git a/sc2/src/libs/graphics/sdl/pure.c b/sc2/src/libs/graphics/sdl/pure.c index 96a7ecf66..423443b0d 100644 --- a/sc2/src/libs/graphics/sdl/pure.c +++ b/sc2/src/libs/graphics/sdl/pure.c @@ -263,6 +263,14 @@ TFB_Pure_InitGraphics (int driver, int flags, int width, int height) return 0; } +void +TFB_Pure_UninitGraphics (void) +{ + UnInit_Screen (&scaled_display); + UnInit_Screen (&fade_color_surface); + UnInit_Screen (&fade_temp); +} + static void ScanLines (SDL_Surface *dst, SDL_Rect *r) { diff --git a/sc2/src/libs/graphics/sdl/pure.h b/sc2/src/libs/graphics/sdl/pure.h index 2538c3f0d..8e2632a0a 100644 --- a/sc2/src/libs/graphics/sdl/pure.h +++ b/sc2/src/libs/graphics/sdl/pure.h @@ -22,6 +22,7 @@ #include "libs/graphics/sdl/sdl_common.h" int TFB_Pure_InitGraphics (int driver, int flags, int width, int height); +void TFB_Pure_UninitGraphics (void); int TFB_Pure_ConfigureVideo (int driver, int flags, int width, int height, int togglefullscreen); void Scale_PerfTest (void); diff --git a/sc2/src/libs/graphics/sdl/sdl_common.c b/sc2/src/libs/graphics/sdl/sdl_common.c index 389ed73b3..64667f1f0 100644 --- a/sc2/src/libs/graphics/sdl/sdl_common.c +++ b/sc2/src/libs/graphics/sdl/sdl_common.c @@ -52,6 +52,8 @@ TFB_GRAPHICS_BACKEND *graphics_backend = NULL; volatile int QuitPosted = 0; volatile int GameActive = 1; // Track the SDL_ACTIVEEVENT state SDL_APPACTIVE +static void TFB_PreQuit (void); + void TFB_PreInit (void) { @@ -76,6 +78,14 @@ TFB_PreInit (void) log_add (log_Fatal, "Could not initialize SDL: %s.", SDL_GetError ()); exit (EXIT_FAILURE); } + + atexit (TFB_PreQuit); +} + +static void +TFB_PreQuit (void) +{ + SDL_Quit (); } int @@ -129,15 +139,9 @@ TFB_ReInitGraphics (int driver, int flags, int width, int height) int TFB_InitGraphics (int driver, int flags, int width, int height) { - int result, i; + int result; char caption[200]; - /* Null out screen pointers the first time */ - for (i = 0; i < TFB_GFX_NUMSCREENS; i++) - { - SDL_Screens[i] = NULL; - } - GfxFlags = flags; if (driver == TFB_GFXDRIVER_SDL_OPENGL) @@ -168,17 +172,25 @@ TFB_InitGraphics (int driver, int flags, int width, int height) TFB_DrawCanvas_Initialize (); - atexit (TFB_UninitGraphics); - return 0; } void TFB_UninitGraphics (void) { + int i; + Uninit_DrawCommandQueue (); - // TODO: Uninit whatever the drivers have set up for us - SDL_Quit (); + + for (i = 0; i < TFB_GFX_NUMSCREENS; i++) + UnInit_Screen (&SDL_Screens[i]); + + TFB_Pure_UninitGraphics (); +#ifdef HAVE_OPENGL + TFB_GL_UninitGraphics (); +#endif + + UnInit_Screen (&format_conv_surf); } void @@ -370,9 +382,18 @@ Create_Screen (SDL_Surface *templat, int w, int h) int ReInit_Screen (SDL_Surface **screen, SDL_Surface *templat, int w, int h) { - if (*screen) - SDL_FreeSurface (*screen); + UnInit_Screen (screen); *screen = Create_Screen (templat, w, h); return *screen == 0 ? -1 : 0; } + +void +UnInit_Screen (SDL_Surface **screen) +{ + if (*screen == NULL) + return; + + SDL_FreeSurface (*screen); + *screen = NULL; +} diff --git a/sc2/src/libs/graphics/sdl/sdl_common.h b/sc2/src/libs/graphics/sdl/sdl_common.h index 091bcde3f..d37800db3 100644 --- a/sc2/src/libs/graphics/sdl/sdl_common.h +++ b/sc2/src/libs/graphics/sdl/sdl_common.h @@ -50,5 +50,6 @@ SDL_Surface* TFB_DisplayFormatAlpha (SDL_Surface *surface); SDL_Surface* Create_Screen (SDL_Surface *templat, int w, int h); int ReInit_Screen (SDL_Surface **screen, SDL_Surface *templat, int w, int h); +void UnInit_Screen (SDL_Surface **screen); #endif diff --git a/sc2/src/libs/input/sdl/input.c b/sc2/src/libs/input/sdl/input.c index f8cc1c859..3bcfc502b 100644 --- a/sc2/src/libs/input/sdl/input.c +++ b/sc2/src/libs/input/sdl/input.c @@ -273,7 +273,6 @@ TFB_InitInput (int driver, int flags) VControl_ResetInput (); InputInitialized = TRUE; - atexit (TFB_UninitInput); return 0; } diff --git a/sc2/src/libs/log/uqmlog.c b/sc2/src/libs/log/uqmlog.c index 5e9cc3dd5..e054edb40 100644 --- a/sc2/src/libs/log/uqmlog.c +++ b/sc2/src/libs/log/uqmlog.c @@ -159,6 +159,7 @@ log_exit (int code) { qlock = 0; DestroyMutex (qmutex); + qmutex = 0; } return code; diff --git a/sc2/src/libs/sound/mixer/nosound/audiodrv_nosound.c b/sc2/src/libs/sound/mixer/nosound/audiodrv_nosound.c index c879c19cb..005bb4466 100644 --- a/sc2/src/libs/sound/mixer/nosound/audiodrv_nosound.c +++ b/sc2/src/libs/sound/mixer/nosound/audiodrv_nosound.c @@ -146,8 +146,6 @@ noSound_Init (audio_Driver *driver, sint32 flags) return -1; } - atexit (unInitAudio); - PlaybackTask = AssignTask (PlaybackTaskFunc, 1024, "nosound audio playback"); diff --git a/sc2/src/libs/sound/mixer/sdl/audiodrv_sdl.c b/sc2/src/libs/sound/mixer/sdl/audiodrv_sdl.c index 249927a0d..5b02dcafa 100644 --- a/sc2/src/libs/sound/mixer/sdl/audiodrv_sdl.c +++ b/sc2/src/libs/sound/mixer/sdl/audiodrv_sdl.c @@ -212,8 +212,6 @@ mixSDL_Init (audio_Driver *driver, sint32 flags) return -1; } - atexit (unInitAudio); - SDL_PauseAudio (0); return 0; @@ -239,6 +237,7 @@ mixSDL_Uninit (void) HFree (sbuffer); } DestroyMutex (soundSource[i].stream_mutex); + soundSource[i].stream_mutex = 0; mixSDL_DeleteSources (1, &soundSource[i].handle); } diff --git a/sc2/src/libs/sound/openal/audiodrv_openal.c b/sc2/src/libs/sound/openal/audiodrv_openal.c index f517f7a88..eee1cd1d8 100644 --- a/sc2/src/libs/sound/openal/audiodrv_openal.c +++ b/sc2/src/libs/sound/openal/audiodrv_openal.c @@ -126,7 +126,6 @@ openAL_Init (audio_Driver *driver, sint32 flags) } *driver = openAL_Driver; - atexit (unInitAudio); alcContext = alcCreateContext (alcDevice, NULL); if (!alcContext) diff --git a/sc2/src/libs/task/tasklib.c b/sc2/src/libs/task/tasklib.c index f88b48afe..81f77aff1 100644 --- a/sc2/src/libs/task/tasklib.c +++ b/sc2/src/libs/task/tasklib.c @@ -124,7 +124,6 @@ InitTaskSystem (void) { task_array[i].state_mutex = CreateMutex ("task manager lock", SYNC_CLASS_TOPLEVEL | SYNC_CLASS_RESOURCE); } - atexit (CleanupTaskSystem); } void @@ -134,6 +133,7 @@ CleanupTaskSystem (void) for (i = 0; i < TASK_MAX; ++i) { DestroyMutex (task_array[i].state_mutex); + task_array[i].state_mutex = 0; } } diff --git a/sc2/src/uqm.c b/sc2/src/uqm.c index 67f3716c2..50db47fac 100644 --- a/sc2/src/uqm.c +++ b/sc2/src/uqm.c @@ -464,14 +464,14 @@ main (int argc, char *argv[]) * tasks might still be using it */ if (MainExited) { - // Not yet: TFB_UninitInput (); + TFB_UninitInput (); unInitAudio (); uninit_communication (); - // TODO: Merge into TFB_UninitGraphics when it goes live + TFB_PurgeDanglingGraphics (); // Purge above refers to colormaps which have to be still up UninitColorMaps (); - // Not yet: TFB_UninitGraphics (); + TFB_UninitGraphics (); #ifdef NETPLAY NetManager_uninit (); @@ -481,7 +481,7 @@ main (int argc, char *argv[]) Callback_uninit (); Alarm_uninit (); - // Not yet: CleanupTaskSystem (); + CleanupTaskSystem (); UnInitTimeSystem (); #if 0 unInitTempDir (); diff --git a/sc2/src/uqm/globdata.c b/sc2/src/uqm/globdata.c index 69b7883ff..20e39a470 100644 --- a/sc2/src/uqm/globdata.c +++ b/sc2/src/uqm/globdata.c @@ -46,8 +46,6 @@ FRAME PlayFrame; GLOBDATA GlobData; -static BOOLEAN initedGameStructs = FALSE; - BYTE getGameState (int startBit, int endBit) @@ -347,17 +345,6 @@ InitGameStructures (void) GLOBAL (autopilot.x) = ~0; GLOBAL (autopilot.y) = ~0; - /* In case the program is exited before the full game is terminated, - * make sure that the temporary files are deleted. - * This can be removed if we make sure if the full game is terminated - * before the game is exited. - * The initedSIS variable is added so the uninit won't happen more - * than once, as you can't remove the atexit function (when the full game - * ends). - */ - initedGameStructs = TRUE; - atexit (UninitGameStructures); - return (TRUE); } @@ -379,9 +366,6 @@ UninitGameStructures (void) { HFLEETINFO hStarShip; - if (!initedGameStructs) - return; - UninitQueue (&GLOBAL (encounter_q)); UninitQueue (&GLOBAL (ip_group_q)); UninitQueue (&GLOBAL (npc_built_ship_q)); @@ -409,7 +393,6 @@ UninitGameStructures (void) DestroyDrawable (ReleaseDrawable (PlayFrame)); PlayFrame = 0; - initedGameStructs = FALSE; } void