Skip to content

Commit

Permalink
Fixed memory leak in TextureManager, surfaces didn't get SDL_FreeSurf…
Browse files Browse the repository at this point in the history
…ace()'ed
  • Loading branch information
Grumbel committed Aug 12, 2014
1 parent b2afe9f commit 441f002
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
56 changes: 32 additions & 24 deletions src/video/texture_manager.cpp
Expand Up @@ -54,6 +54,12 @@ TextureManager::~TextureManager()
}
}
image_textures.clear();

for(auto& surface : surfaces)
{
SDL_FreeSurface(surface.second);
}
surfaces.clear();
}

TexturePtr
Expand Down Expand Up @@ -109,7 +115,7 @@ TextureManager::remove_texture(GLTexture* texture)
TexturePtr
TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
{
try
try
{
return create_image_texture_raw(filename, rect);
}
Expand All @@ -123,29 +129,29 @@ TextureManager::create_image_texture(const std::string& filename, const Rect& re
TexturePtr
TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
{
SDL_Surface *image = NULL;
SDL_Surface *image = nullptr;

Surfaces::iterator i = surfaces.find(filename);
if (i != surfaces.end())
{
image = i->second;

if (!image) {
image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
}

if (!image)
else
{
std::ostringstream msg;
msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
throw std::runtime_error(msg.str());
}

surfaces[filename] = image;
image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
if (!image)
{
std::ostringstream msg;
msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
throw std::runtime_error(msg.str());
}

SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
rect.top * image->pitch +
rect.left * image->format->BytesPerPixel,
surfaces[filename] = image;
}

SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
rect.top * image->pitch +
rect.left * image->format->BytesPerPixel,
rect.get_width(), rect.get_height(),
image->format->BitsPerPixel,
image->pitch,
Expand All @@ -158,18 +164,20 @@ TextureManager::create_image_texture_raw(const std::string& filename, const Rect
throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
}

/* if (image->format->palette)
#ifdef OLD_SDL
if (image->format->palette)
{ // copy the image palette to subimage if present
SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors); //edited by giby
} */
SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors);
}
#endif

return VideoSystem::new_texture(subimage.get());
}

TexturePtr
TextureManager::create_image_texture(const std::string& filename)
{
try
try
{
return create_image_texture_raw(filename);
}
Expand All @@ -184,7 +192,7 @@ TexturePtr
TextureManager::create_image_texture_raw(const std::string& filename)
{
SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
if (!image)
if (!image)
{
std::ostringstream msg;
msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
Expand All @@ -202,14 +210,14 @@ TexturePtr
TextureManager::create_dummy_texture()
{
const std::string dummy_texture_fname = "images/engine/missing.png";

// on error, try loading placeholder file
try
try
{
TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
return tex;
}
catch (const std::exception& err)
catch (const std::exception& err)
{
// on error (when loading placeholder), try using empty surface,
// when that fails to, just give up
Expand Down
2 changes: 1 addition & 1 deletion src/video/texture_manager.hpp
Expand Up @@ -70,7 +70,7 @@ class TextureManager
TexturePtr create_image_texture_raw(const std::string& filename, const Rect& rect);

TexturePtr create_dummy_texture();

#ifdef HAVE_OPENGL
typedef std::set<GLTexture*> Textures;
Textures textures;
Expand Down

0 comments on commit 441f002

Please sign in to comment.