Anonymous avatar Anonymous committed 38b8ae6

more freeing SDL objects when python malloc fails

Comments (0)

Files changed (5)

 /*BAD things happen when out-of-bound rects go to updaterect*/
 static int screencroprect(GAME_Rect* r, int w, int h)
 {
-	if(r->x >= w || r->y >= h || (r->x + w->w) < 0 || (r->y + r->h) < 0)
+	if(r->x >= w || r->y >= h || (r->x + r->w) < 0 || (r->y + r->h) < 0)
 		return 0;
 	else
 	{
 {
 	TTF_Font* font = PyFont_AsFont(self);
 	int aa;
-	PyObject* text;
+	PyObject* text, *final;
 	PyObject* fg_rgba_obj, *bg_rgba_obj = NULL;
 	Uint8 rgba[4];
 	SDL_Surface* surf;
 		surf->format->palette->colors[0].b = backg.b;
 	}
 
-	return PySurface_New(surf);
+	final = PySurface_New(surf);
+	if(!final)
+		SDL_FreeSurface(surf);
+	return final;
 }
 
 
 	
 	if(!font)
 		return RAISE(PyExc_RuntimeError, SDL_GetError());
+
 	fontobj = PyFont_New(font);
+	if(!fontobj)
+		TTF_CloseFont(font);
 	return fontobj;
 }
 
 
 	fontobj = PyObject_NEW(PyFontObject, &PyFont_Type);
 	if(fontobj)
-	{
 		fontobj->font = font;
-	}
-	else
-	{
-		/*free the original font, nobody else can*/
-		TTF_CloseFont(font); 
-	}
 
 	return (PyObject*)fontobj;
 }
 
 static PyObject* load(PyObject* self, PyObject* arg)
 {
-	PyObject* file;
+	PyObject* file, *final;
 	char* name = NULL;
 	SDL_Surface* surf;
 	SDL_RWops *rw;
 	if(!surf)
 		return RAISE(PyExc_SDLError, IMG_GetError());
 
-	return PySurface_New(surf);
+	final = PySurface_New(surf);
+	if(!final)
+		SDL_FreeSurface(surf);
+	return final;
 }
 
 
 
 static PyObject* Sound(PyObject* self, PyObject* arg)
 {
-	PyObject* file;
+	PyObject* file, *final;
 	char* name = NULL;
 	Mix_Chunk* chunk;
 	SDL_RWops *rw;
 	if(!chunk)
 		return RAISE(PyExc_SDLError, SDL_GetError());
 
-	return PySound_New(chunk);
+	final = PySound_New(chunk);
+	if(!final)
+		Mix_FreeChunk(chunk);
+	return final;
 }
 
 
 
 	soundobj = PyObject_NEW(PySoundObject, &PySound_Type);
 	if(soundobj)
-	{
 		soundobj->chunk = chunk;
-	}
-	else
-	{
-		/*free the sound here, nobody else can*/
-		Mix_FreeChunk(chunk);
-	}
 
 	return (PyObject*)soundobj;
 }
 static PyObject* surf_convert(PyObject* self, PyObject* args)
 {
 	SDL_Surface* surf = PySurface_AsSurface(self);
+	PyObject* final;
 	PySurfaceObject* srcsurf = NULL;
 	SDL_Surface* src;
 	SDL_Surface* newsurf;
 		newsurf = SDL_DisplayFormat(surf);
 	PySurface_Unprep(self);
 
-	return PySurface_New(newsurf);
+	final = PySurface_New(newsurf);
+	if(!final)
+		SDL_FreeSurface(newsurf);
+	return final;
 }
 
 
 static PyObject* surf_convert_alpha(PyObject* self, PyObject* args)
 {
 	SDL_Surface* surf = PySurface_AsSurface(self);
+	PyObject* final;
 	PySurfaceObject* srcsurf = NULL;
 	SDL_Surface* newsurf, *src;
 	
 		newsurf = SDL_DisplayFormatAlpha(surf);
 	PySurface_Unprep(self);
 
-	return PySurface_New(newsurf);
+	final = PySurface_New(newsurf);
+	if(!final)
+		SDL_FreeSurface(newsurf);
+	return final;
 }
 
 
 		surf->didlock = 0;
 		surf->lockcount = 0;
 	}
-	else
-	{
-		/*free the surface here, because no one else can*/
-		SDL_FreeSurface(s);
-	}
 	return (PyObject*)surf;
 }
 
 {
 	Uint32 flags = 0;
 	int width, height;
-	PyObject *depth=NULL, *masks=NULL;
+	PyObject *depth=NULL, *masks=NULL, *final;
 	short bpp;
 	Uint32 Rmask, Gmask, Bmask, Amask;
 	SDL_Surface* surface;
 	}
 
 	surface = SDL_CreateRGBSurface(flags, width, height, bpp, Rmask, Gmask, Bmask, Amask);
-	return PySurface_New(surface);
+	final = PySurface_New(surface);
+	if(!final)
+		SDL_FreeSurface(surface);
+	return final;
 }
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.