From 9311432cbfe7967c08d794efa95f0d625c09435c Mon Sep 17 00:00:00 2001 From: helixhorned Date: Wed, 5 Sep 2012 17:25:31 +0000 Subject: [PATCH] Fix crash when recording demo, stooping it, going to title screen, and exiting. A macro MAYBE_FCLOSE_AND_NULL(fileptr) is introduced in compat.h that does what it says. git-svn-id: https://svn.eduke32.com/eduke32@2995 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/include/compat.h | 4 ++++ polymer/eduke32/source/demo.c | 6 +++--- polymer/eduke32/source/game.c | 9 +++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/polymer/eduke32/build/include/compat.h b/polymer/eduke32/build/include/compat.h index 92357af45..11c12ff1f 100644 --- a/polymer/eduke32/build/include/compat.h +++ b/polymer/eduke32/build/include/compat.h @@ -650,5 +650,9 @@ char *Bstrupr(char *); } #endif +#define MAYBE_FCLOSE_AND_NULL(fileptr) do { \ + if (fileptr) { Bfclose(fileptr); fileptr=NULL; } \ +} while (0) + #endif // __compat_h__ diff --git a/polymer/eduke32/source/demo.c b/polymer/eduke32/source/demo.c index f64673252..e6d076ceb 100644 --- a/polymer/eduke32/source/demo.c +++ b/polymer/eduke32/source/demo.c @@ -193,7 +193,7 @@ void G_OpenDemoWrite(void) if (g_demo_filePtr == NULL) break; - Bfclose(g_demo_filePtr); + MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); } while (1); @@ -205,7 +205,7 @@ void G_OpenDemoWrite(void) demorec_synccompress_cvar|(demorec_seeds_cvar<<1)); if (i) { - Bfclose(g_demo_filePtr), g_demo_filePtr=NULL; + MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); error_wopen_demo: Bstrcpy(ScriptQuotes[QUOTE_RESERVED4], "FAILED STARTING DEMO RECORDING. SEE OSD FOR DETAILS."); P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); @@ -292,7 +292,7 @@ void G_CloseDemoWrite(void) fwrite(&g_demo_cnt, sizeof(g_demo_cnt), 1, g_demo_filePtr); ud.recstat = ud.m_recstat = 0; - fclose(g_demo_filePtr); + MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); sv_freemem(); diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c index 9e841d6fb..b836db66c 100644 --- a/polymer/eduke32/source/game.c +++ b/polymer/eduke32/source/game.c @@ -2077,11 +2077,12 @@ void G_GameExit(const char *msg) #endif if (*msg != 0) g_player[myconnectindex].ps->palette = BASEPAL; - if (ud.recstat == 1) G_CloseDemoWrite(); + if (ud.recstat == 1) + G_CloseDemoWrite(); else if (ud.recstat == 2) - { - if (g_demo_filePtr) fclose(g_demo_filePtr); - } // JBF: fixes crash on demo playback + MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); + // JBF: fixes crash on demo playback + // PK: modified from original if (!g_quickExit) {