From e99b2f596508d8954bb59b4ef9c4061e4c764a04 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Tue, 11 Nov 2003 23:19:40 +0000 Subject: [PATCH] fix memleaks --- stepmania/src/SDL_utils.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/stepmania/src/SDL_utils.cpp b/stepmania/src/SDL_utils.cpp index d15a6df7eb..54d7c919ca 100644 --- a/stepmania/src/SDL_utils.cpp +++ b/stepmania/src/SDL_utils.cpp @@ -588,21 +588,32 @@ SDL_Surface *mySDL_LoadSurface( CString file ) return NULL; SurfaceHeader h; - if(fread(&h, sizeof(h), 1, f) != 1) return NULL; - + if( fread(&h, sizeof(h), 1, f) != 1 ) + { + fclose(f); + return NULL; + } + SDL_Color palette[256]; if(h.bpp == 8) - if(fread(palette, 256 * sizeof(SDL_Color), 1, f) != 1) return NULL; - - int size = h.height * h.pitch; - char *pixels = (char *) malloc(size); - if(fread(pixels, size, 1, f) != 1) return NULL; + if( fread(palette, 256 * sizeof(SDL_Color), 1, f) != 1 ) + { + fclose(f); + return NULL; + } /* Create the surface. */ - SDL_Surface *img = SDL_CreateRGBSurfaceFrom(pixels, + SDL_Surface *img = SDL_CreateRGBSurface( h.width, h.height, h.bpp, h.pitch, h.Rmask, h.Gmask, h.Bmask, h.Amask); + if( fread(img->pixels, h.height * h.pitch, 1, f) != 1 ) + { + fclose(f); + SDL_FreeSurface( img ); + return NULL; + } + /* Set the palette. */ if( h.bpp == 8 ) {