ref: 5607efe36e46f5cc181fd294ce275909139795b5
parent: 1659e586b4ded9f6bfaaf6ee246e4888725c7945
author: Alex Mayfield <[email protected]>
date: Tue Feb 21 15:29:16 EST 2017
Free resources that we use
--- a/midiproc/main.c
+++ b/midiproc/main.c
@@ -50,6 +50,23 @@
//
//
+// Cleanly close our in-use pipes.
+//
+static void FreePipes(void)
+{
+ if (midi_process_in != NULL)
+ {
+ CloseHandle(midi_process_in);
+ midi_process_in = NULL;
+ }
+ if (midi_process_out != NULL)
+ {
+ CloseHandle(midi_process_out);
+ midi_process_out = NULL;
+ }
+}
+
+//
// Unregisters the currently playing song. This is never called from the
// protocol, we simply do this before playing a new song.
//
@@ -64,7 +81,7 @@
}
//
-// Bookkeeping stuff we need to do when we're shutting off the subprocess.
+// Cleanly shut down SDL.
//
static void ShutdownSDL(void)
{
@@ -343,16 +360,23 @@
midi_process_in = GetStdHandle(STD_INPUT_HANDLE);
if (midi_process_in == INVALID_HANDLE_VALUE)
{
- return false;
+ goto fail;
}
midi_process_out = GetStdHandle(STD_OUTPUT_HANDLE);
if (midi_process_out == INVALID_HANDLE_VALUE)
{
- return false;
+ goto fail;
}
+ atexit(FreePipes);
+
return true;
+
+fail:
+ FreePipes();
+
+ return false;
}
//
--- a/src/i_midipipe.c
+++ b/src/i_midipipe.c
@@ -66,6 +66,35 @@
//
//
+// FreePipes
+//
+// Free all pipes in use by this module.
+//
+static void FreePipes()
+{
+ if (midi_process_in_reader != NULL)
+ {
+ CloseHandle(midi_process_in_reader);
+ midi_process_in_reader = NULL;
+ }
+ if (midi_process_in_writer != NULL)
+ {
+ CloseHandle(midi_process_in_writer);
+ midi_process_in_writer = NULL;
+ }
+ if (midi_process_out_reader != NULL)
+ {
+ CloseHandle(midi_process_out_reader);
+ midi_process_in_reader = NULL;
+ }
+ if (midi_process_out_writer != NULL)
+ {
+ CloseHandle(midi_process_out_writer);
+ midi_process_out_writer = NULL;
+ }
+}
+
+//
// UsingNativeMidi
//
// Enumerate all music decoders and return true if NATIVEMIDI is one of them.
@@ -161,9 +190,9 @@
// Continue looping as long as we don't exceed our maximum wait time.
} while (start + MIDIPIPE_MAX_WAIT > I_GetTimeMS());
-fail:
- // TODO: Deal with the wedged process.
+fail:
+ // TODO: Deal with the wedged process?
return false;
}
@@ -289,7 +318,7 @@
}
//
-// I_MidiPipe_StopSong
+// I_MidiPipe_ShutdownServer
//
// Tells the MIDI subprocess to shutdown.
//
@@ -303,6 +332,8 @@
ok = WritePipe(packet);
NET_FreePacket(packet);
+ FreePipes();
+
midi_server_initialized = false;
if (!ok)
@@ -328,6 +359,8 @@
{
struct stat sbuf;
char filename[MAX_PATH + 1];
+ char *module = NULL;
+ char *cmdline = NULL;
if (!UsingNativeMidi() || strlen(snd_musiccmd) > 0)
{
@@ -340,7 +373,7 @@
size_t filename_len = GetModuleFileName(NULL, filename, MAX_PATH);
// Remove filespec
- // TODO: Move this to m_misc
+ // TODO: Move this to m_misc?
char *fp = &filename[filename_len];
while (filename <= fp && *fp != DIR_SEPARATOR)
{
@@ -347,10 +380,8 @@
fp--;
}
*(fp + 1) = '\0';
- char* module = M_StringJoin(filename, PROGRAM_PREFIX "midiproc.exe", NULL);
- char* cmdline = M_StringJoin(module, " \"" PACKAGE_STRING "\"", NULL);
- DEBUGOUT(module);
- DEBUGOUT(cmdline);
+ module = M_StringJoin(filename, PROGRAM_PREFIX "midiproc.exe", NULL);
+ cmdline = M_StringJoin(module, " \"" PACKAGE_STRING "\"", NULL);
// Look for executable file
if(stat(module, &sbuf))
@@ -401,20 +432,23 @@
startup_info.hStdOutput = midi_process_out_writer;
startup_info.dwFlags = STARTF_USESTDHANDLES;
- boolean ok = CreateProcess(TEXT(module), TEXT(cmdline), NULL, NULL, TRUE,
+ BOOL ok = CreateProcess(TEXT(module), TEXT(cmdline), NULL, NULL, TRUE,
CREATE_NEW_CONSOLE, NULL, NULL, &startup_info, &proc_info);
- if (ok)
+ if (!ok)
{
- DEBUGOUT("midiproc started");
- midi_server_initialized = true;
+ goto fail;
}
- else
- {
- DEBUGOUT("failed to start midiproc");
- }
- return ok;
+ midi_server_initialized = true;
+ return true;
+
+fail:
+ FreePipes();
+ free(cmdline);
+ free(module);
+
+ return false;
}
#endif