From dd76be16cbe6cfe075376fff1b358e1e57d305b2 Mon Sep 17 00:00:00 2001
From: Monster Iestyn <iestynjealous@ntlworld.com>
Date: Thu, 19 Mar 2020 20:42:51 +0000
Subject: [PATCH] Create G_FreeGhosts, for the benefit of G_DeferedInitNew
 (assuming it actually needs to do `ghosts = NULL;` at all)

---
 src/g_demo.c | 21 +++++++++++++--------
 src/g_demo.h |  1 +
 src/g_game.c |  2 +-
 3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/g_demo.c b/src/g_demo.c
index 22cd34ad5..9adafae54 100644
--- a/src/g_demo.c
+++ b/src/g_demo.c
@@ -2223,6 +2223,18 @@ void G_AddGhost(char *defdemoname)
 	Z_Free(pdemoname);
 }
 
+// Clean up all ghosts
+void G_FreeGhosts(void)
+{
+	while (ghosts)
+	{
+		demoghost *next = ghosts->next;
+		Z_Free(ghosts);
+		ghosts = next;
+	}
+	ghosts = NULL;
+}
+
 //
 // G_TimeDemo
 // NOTE: name is a full filename for external demos
@@ -2389,14 +2401,7 @@ boolean G_CheckDemoStatus(void)
 {
 	boolean saved;
 
-	while (ghosts)
-	{
-		demoghost *next = ghosts->next;
-		Z_Free(ghosts);
-		ghosts = next;
-	}
-	ghosts = NULL;
-
+	G_FreeGhosts();
 
 	// DO NOT end metal sonic demos here
 
diff --git a/src/g_demo.h b/src/g_demo.h
index 89be6e343..df25042c4 100644
--- a/src/g_demo.h
+++ b/src/g_demo.h
@@ -75,6 +75,7 @@ void G_DeferedPlayDemo(const char *demo);
 void G_DoPlayDemo(char *defdemoname);
 void G_TimeDemo(const char *name);
 void G_AddGhost(char *defdemoname);
+void G_FreeGhosts(void);
 void G_DoPlayMetal(void);
 void G_DoneLevelLoad(void);
 void G_StopMetalDemo(void);
diff --git a/src/g_game.c b/src/g_game.c
index fb70e2995..e22961b9a 100644
--- a/src/g_game.c
+++ b/src/g_game.c
@@ -4490,7 +4490,7 @@ void G_DeferedInitNew(boolean pultmode, const char *mapname, INT32 pickedchar, b
 
 	if (demoplayback)
 		COM_BufAddText("stopdemo\n");
-	ghosts = NULL;
+	G_FreeGhosts(); // TODO: do we actually need to do this?
 
 	// this leave the actual game if needed
 	SV_StartSinglePlayerServer();
-- 
GitLab