From a529dca69ffdc93d6db1b40249cb07457e793049 Mon Sep 17 00:00:00 2001
From: Alam Ed Arias <alam@srb2.org>
Date: Tue, 10 May 2016 18:20:14 -0400
Subject: [PATCH] SDL: in the SDL 2.0 interface code, we use a fake surface as
 our vidsurface, so we should be freeing it

---
 src/Makefile      |  2 +-
 src/sdl/i_video.c | 17 ++++++++++-------
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/Makefile b/src/Makefile
index 0c034143d9..b6930ffd9f 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -372,7 +372,7 @@ else
 
 	# build a normal optimised version
 	WINDRESFLAGS = -DNDEBUG
-	CFLAGS+=-O2
+	#CFLAGS+=-O2
 endif
 	CFLAGS+=-g $(OPTS) $(M5) $(WINDRESFLAGS)
 
diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c
index 8d345d2a8b..0a0aaf312b 100644
--- a/src/sdl/i_video.c
+++ b/src/sdl/i_video.c
@@ -251,6 +251,11 @@ static void SDLSetMode(INT32 width, INT32 height, SDL_bool fullscreen)
 		{
 			SDL_FreeSurface(vidSurface);
 		}
+		if (vid.buffer)
+		{
+			free(vid.buffer);
+			vid.buffer = NULL;
+		}
 		SDL_PixelFormatEnumToMasks(sw_texture_format, &bpp, &rmask, &gmask, &bmask, &amask);
 		vidSurface = SDL_CreateRGBSurface(0, width, height, bpp, rmask, gmask, bmask, amask);
 	}
@@ -1654,12 +1659,10 @@ static void Impl_VideoSetupBuffer(void)
 	{
 		vid.rowbytes = vid.width * vid.bpp;
 		vid.direct = NULL;
-		vid.buffer = malloc(vid.rowbytes*vid.height*NUMSCREENS);
 		if (vid.buffer)
-		{
-			memset(vid.buffer,0x00,vid.rowbytes*vid.height*NUMSCREENS);
-		}
-		else
+			free(vid.buffer);
+		vid.buffer = calloc(vid.rowbytes*vid.height, NUMSCREENS);
+		if (!vid.buffer)
 		{
 			I_Error("%s", M_GetText("Not enough memory for video buffer\n"));
 		}
@@ -1766,7 +1769,6 @@ void I_StartupGraphics(void)
 	//Impl_SetWindowName("SRB2 "VERSIONSTRING);
 	VID_SetMode(VID_GetModeForSize(BASEVIDWIDTH, BASEVIDHEIGHT));
 
-	vid.buffer = NULL;  // For software mode
 	vid.width = BASEVIDWIDTH; // Default size for startup
 	vid.height = BASEVIDHEIGHT; // BitsPerPixel is the SDL interface's
 	vid.recalc = true; // Set up the console stufff
@@ -1816,7 +1818,8 @@ void I_ShutdownGraphics(void)
 	icoSurface = NULL;
 	if (render_soft == oldrendermode)
 	{
-		vidSurface = NULL; //Alam: SDL_Video system free vidSurface for me
+		if (vidSurface) SDL_FreeSurface(vidSurface);
+		vidSurface = NULL;
 		if (vid.buffer) free(vid.buffer);
 		vid.buffer = NULL;
 		if (bufSurface) SDL_FreeSurface(bufSurface);
-- 
GitLab