From c9ff8ec26b4ac47190a5a78f820366ec9afcaba4 Mon Sep 17 00:00:00 2001 From: Jaime Passos Date: Sun, 24 Nov 2019 17:37:11 -0300 Subject: [PATCH] Fix FreeMipmapColormap crash --- src/hardware/hw_cache.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/hardware/hw_cache.c b/src/hardware/hw_cache.c index 6eaafca6d..2d4704b8b 100644 --- a/src/hardware/hw_cache.c +++ b/src/hardware/hw_cache.c @@ -771,18 +771,25 @@ void HWR_InitTextureCache(void) gr_textures2 = NULL; } - // Callback function for HWR_FreeTextureCache. static void FreeMipmapColormap(INT32 patchnum, void *patch) { - GLPatch_t* const grpatch = patch; + GLPatch_t* const pat = patch; (void)patchnum; //unused - while (grpatch->mipmap->nextcolormap) + while (pat->mipmap && pat->mipmap->nextcolormap) // The mipmap must be valid, obviously { - GLMipmap_t *grmip = grpatch->mipmap->nextcolormap; - grpatch->mipmap->nextcolormap = grmip->nextcolormap; - if (grmip->grInfo.data) Z_Free(grmip->grInfo.data); - free(grmip); + // Confusing at first, but pat->mipmap->nextcolormap + // at the beginning of the loop is the first colormap + // from the linked list of colormaps + GLMipmap_t *next = pat->mipmap->nextcolormap; + // Set the first colormap + // to the one that comes after it + pat->mipmap->nextcolormap = next->nextcolormap; + // Free image data from memory + if (next->grInfo.data) + Z_Free(next->grInfo.data); + // Free the old colormap from memory + free(next); } } @@ -799,7 +806,7 @@ void HWR_FreeTextureCache(void) // Alam: free the Z_Blocks before freeing it's users - // free all skin after each level: must be done after pfnClearMipMapCache! + // free all patch colormaps after each level: must be done after ClearMipMapCache! for (i = 0; i < numwadfiles; i++) M_AATreeIterate(wadfiles[i]->hwrcache, FreeMipmapColormap);