From 9d3b4459ee329818432390775fbc0d62c3fca08d Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Fri, 23 Jul 2021 18:13:43 -0700 Subject: [PATCH] GB Video: Fix memory leak when reseting SGB games --- CHANGES | 1 + src/gb/video.c | 53 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/CHANGES b/CHANGES index ca9bc224b..592a474c8 100644 --- a/CHANGES +++ b/CHANGES @@ -15,6 +15,7 @@ Emulation fixes: - GBA SIO: Fix SI value for unattached MULTI mode Other fixes: - Core: Don't attempt to restore rewind diffs past start of rewind + - GB Video: Fix memory leak when reseting SGB games - GBA: Fix out of bounds ROM accesses on patched ROMs smaller than 32 MiB Misc: - Core: Suspend runloop when a core crashes diff --git a/src/gb/video.c b/src/gb/video.c index cd0d28778..98c83da4b 100644 --- a/src/gb/video.c +++ b/src/gb/video.c @@ -87,20 +87,53 @@ void GBVideoReset(struct GBVideo* video) { memset(&video->palette, 0, sizeof(video->palette)); if (video->p->model & GB_MODEL_SGB) { - video->renderer->sgbCharRam = anonymousMemoryMap(SGB_SIZE_CHAR_RAM); - video->renderer->sgbMapRam = anonymousMemoryMap(SGB_SIZE_MAP_RAM); - video->renderer->sgbPalRam = anonymousMemoryMap(SGB_SIZE_PAL_RAM); - video->renderer->sgbAttributeFiles = anonymousMemoryMap(SGB_SIZE_ATF_RAM); - video->renderer->sgbAttributes = malloc(90 * 45); + if (video->renderer->sgbCharRam) { + memset(video->renderer->sgbCharRam, 0, SGB_SIZE_CHAR_RAM); + } else { + video->renderer->sgbCharRam = anonymousMemoryMap(SGB_SIZE_CHAR_RAM); + } + if (video->renderer->sgbMapRam) { + memset(video->renderer->sgbMapRam, 0, SGB_SIZE_MAP_RAM); + } else { + video->renderer->sgbMapRam = anonymousMemoryMap(SGB_SIZE_MAP_RAM); + } + if (video->renderer->sgbPalRam) { + memset(video->renderer->sgbPalRam, 0, SGB_SIZE_PAL_RAM); + } else { + video->renderer->sgbPalRam = anonymousMemoryMap(SGB_SIZE_PAL_RAM); + } + if (video->renderer->sgbAttributeFiles) { + memset(video->renderer->sgbAttributeFiles, 0, SGB_SIZE_ATF_RAM); + } else { + video->renderer->sgbAttributeFiles = anonymousMemoryMap(SGB_SIZE_ATF_RAM); + } + if (!video->renderer->sgbAttributes) { + video->renderer->sgbAttributes = malloc(90 * 45); + } memset(video->renderer->sgbAttributes, 0, 90 * 45); video->sgbCommandHeader = 0; video->sgbBufferIndex = 0; } else { - video->renderer->sgbCharRam = NULL; - video->renderer->sgbMapRam = NULL; - video->renderer->sgbPalRam = NULL; - video->renderer->sgbAttributes = NULL; - video->renderer->sgbAttributeFiles = NULL; + if (video->renderer->sgbCharRam) { + mappedMemoryFree(video->renderer->sgbCharRam, SGB_SIZE_CHAR_RAM); + video->renderer->sgbCharRam = NULL; + } + if (video->renderer->sgbMapRam) { + mappedMemoryFree(video->renderer->sgbMapRam, SGB_SIZE_MAP_RAM); + video->renderer->sgbMapRam = NULL; + } + if (video->renderer->sgbPalRam) { + mappedMemoryFree(video->renderer->sgbPalRam, SGB_SIZE_PAL_RAM); + video->renderer->sgbPalRam = NULL; + } + if (video->renderer->sgbAttributeFiles) { + mappedMemoryFree(video->renderer->sgbAttributeFiles, SGB_SIZE_ATF_RAM); + video->renderer->sgbAttributeFiles = NULL; + } + if (video->renderer->sgbAttributes) { + free(video->renderer->sgbAttributes); + video->renderer->sgbAttributes = NULL; + } } video->palette[0] = video->dmgPalette[0];