diff options
author | Andrei Karas <akaras@inbox.ru> | 2011-06-29 23:52:17 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2011-06-30 00:10:21 +0300 |
commit | 63bed529c78ca05d3e32a0b6ec3bcfd6152e53de (patch) | |
tree | 9d233f3ecf71452123f45b4563fef8be4308769b | |
parent | 27300ff284bbed3b6bce938071c3982e4a757680 (diff) | |
download | manaplus-63bed529c78ca05d3e32a0b6ec3bcfd6152e53de.tar.gz manaplus-63bed529c78ca05d3e32a0b6ec3bcfd6152e53de.tar.bz2 manaplus-63bed529c78ca05d3e32a0b6ec3bcfd6152e53de.tar.xz manaplus-63bed529c78ca05d3e32a0b6ec3bcfd6152e53de.zip |
Fix memory leak in desktop.cpp
-rw-r--r-- | src/gui/widgets/desktop.cpp | 16 | ||||
-rw-r--r-- | src/resources/image.cpp | 3 | ||||
-rw-r--r-- | src/resources/resourcemanager.cpp | 31 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 2 |
4 files changed, 40 insertions, 12 deletions
diff --git a/src/gui/widgets/desktop.cpp b/src/gui/widgets/desktop.cpp index f7183553f..6ba6477d6 100644 --- a/src/gui/widgets/desktop.cpp +++ b/src/gui/widgets/desktop.cpp @@ -38,8 +38,8 @@ #include "debug.h" -Desktop::Desktop() - : mWallpaper(0) +Desktop::Desktop() : + mWallpaper(0) { addWidgetListener(this); @@ -132,23 +132,15 @@ void Desktop::setBestFittingWallpaper() || nWallPaper->getHeight() != getHeight())) { // We rescale to obtain a fullscreen wallpaper... - Image *newRsclWlPpr = nWallPaper->SDLgetScaledImage( - getWidth(), getHeight()); - std::string idPath = nWallPaper->getIdPath(); + Image *newRsclWlPpr = ResourceManager::getInstance()->getRescaled( + nWallPaper, getWidth(), getHeight()); // We replace the resource in the resource manager nWallPaper->decRef(); if (newRsclWlPpr) - { - ResourceManager::getInstance()->addResource( - idPath, newRsclWlPpr); - mWallpaper = newRsclWlPpr; - } else - { mWallpaper = nWallPaper; - } } else { diff --git a/src/resources/image.cpp b/src/resources/image.cpp index e9260071f..ec74630ff 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -479,7 +479,10 @@ Image* Image::SDLgetScaledImage(int width, int height) // The load function takes care of the SDL<->OpenGL implementation // and about freeing the given SDL_surface*. if (scaledSurface) + { scaledImage = load(scaledSurface); + SDL_FreeSurface(scaledSurface); + } } return scaledImage; } diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 410611fc3..78ca6a284 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -726,3 +726,34 @@ void ResourceManager::clearScheduled() } deletedSurfaces.clear(); } + +struct RescaledLoader +{ + ResourceManager *manager; + Image *image; + int width; + int height; + static Resource *load(void *v) + { + if (!v) + return NULL; + RescaledLoader *l = static_cast< RescaledLoader * >(v); + if (!l->manager) + return NULL; + Image *rescaled = l->image->SDLgetScaledImage(l->width, l->height); + if (!rescaled) + return NULL; + return rescaled; + } +}; + +Image *ResourceManager::getRescaled(Image *image, int width, int height) +{ + if (!image) + return 0; + + std::string idPath = image->getIdPath() + strprintf("_rescaled%dx%d", width, height); + RescaledLoader l = { this, image, width, height }; + Image *img = static_cast<Image*>(get(idPath, RescaledLoader::load, &l)); + return img; +} diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index 0b73a796e..f70fbece4 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -228,6 +228,8 @@ class ResourceManager void saveTextFile(std::string path, std::string name, std::string text); + Image *getRescaled(Image *image, int width, int height); + /** * Loads the given filename as an SDL surface. The returned surface is * expected to be freed by the caller using SDL_FreeSurface. |