Maelstrom: Fixed lockup at exit, where video cleanup was sometimes happening after Cocoa had cleaned up important things.

From 2a07413996f6e7b5320e5f329b1f81101b2282c3 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 22 Oct 2011 03:15:23 -0400
Subject: [PATCH] Fixed lockup at exit, where video cleanup was sometimes
 happening after Cocoa had cleaned up important things.

---
 init.cpp | 6 +-----
 main.cpp | 5 ++++-
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/init.cpp b/init.cpp
index bb618a40..d3557b5c 100644
--- a/init.cpp
+++ b/init.cpp
@@ -684,7 +684,7 @@ static void BuildVelocityTable(void)
    of the program at any time, including itself if interrupted during
    cleanup.  *sigh*  reentrant multi-threading can be a pain. :)
 */
-extern "C" void CleanUp(void)
+void CleanUp(void)
 {
 	int i;
 
@@ -718,7 +718,6 @@ int DoInitializations(Uint32 video_flags)
 	int i;
 	SDL_Surface *icon;
 
-	/* Make sure we clean up properly at exit */
 	Uint32 init_flags = (SDL_INIT_VIDEO|SDL_INIT_AUDIO);
 #ifdef SDL_INIT_JOYSTICK
 	init_flags |= SDL_INIT_JOYSTICK;
@@ -730,8 +729,6 @@ int DoInitializations(Uint32 video_flags)
 			return(-1);
 		}
 	}
-	atexit(CleanUp);
-	signal(SIGSEGV, exit);
 
 	// -- Initialize some variables
 	gLastHigh = -1;
@@ -766,7 +763,6 @@ int DoInitializations(Uint32 video_flags)
 	screen->SetCaption("Maelstrom");
 	screen->Clear();
 	screen->Update();
-	atexit(CleanUp);		// Need to reset this under X11 DGA
 	SDL_FreeSurface(icon);
 
 	/* Load the Font Server and fonts */
diff --git a/main.cpp b/main.cpp
index 6ac56294..10465ef7 100644
--- a/main.cpp
+++ b/main.cpp
@@ -17,7 +17,8 @@
 #include "checksum.h"
 
 /* External functions used in this file */
-extern int DoInitializations(Uint32 video_flags);		/* init.cc */
+extern int DoInitializations(Uint32 video_flags);		/* init.cpp */
+extern void CleanUp(void);
 
 static const char *Version =
 "Maelstrom v1.4.3 (GPL version 4.0.0) -- 10/08/2011 by Sam Lantinga\n";
@@ -298,6 +299,7 @@ int main(int argc, char *argv[])
 
 	if ( speedtest ) {
 		RunSpeedTest();
+		CleanUp();
 		exit(0);
 	}
 
@@ -485,6 +487,7 @@ void DrawMainScreen(void)
 	title = Load_Title(screen, 129);
 	if ( title == NULL ) {
 		error("Can't load 'title' title! (ID=%d)\n", 129);
+		CleanUp();
 		exit(255);
         }