]> git.lizzy.rs Git - minetest.git/commitdiff
guiScalingFilter: Fix most memory leaks (#12256)
authorSmallJoker <SmallJoker@users.noreply.github.com>
Wed, 4 May 2022 18:55:13 +0000 (20:55 +0200)
committerGitHub <noreply@github.com>
Wed, 4 May 2022 18:55:13 +0000 (20:55 +0200)
Calls to the cache function ended up creating a new texture regardless whether
the texture is already cached.

src/client/client.cpp
src/client/guiscalingfilter.cpp

index 0a1fc73d14487d524601b8ddd88196c7a09a8c5f..cb556c1ce5cf56b5fd3250b66a8f319bed4cafd7 100644 (file)
@@ -334,6 +334,8 @@ Client::~Client()
        // cleanup 3d model meshes on client shutdown
        m_rendering_engine->cleanupMeshCache();
 
+       guiScalingCacheClear();
+
        delete m_minimap;
        m_minimap = nullptr;
 
index 232219237be2bdcab84745022b29f35cd0c358d2..de122becf218942737f78109727c67866cb60de7 100644 (file)
@@ -43,6 +43,10 @@ void guiScalingCache(const io::path &key, video::IVideoDriver *driver, video::II
 {
        if (!g_settings->getBool("gui_scaling_filter"))
                return;
+
+       if (g_imgCache.find(key) != g_imgCache.end())
+               return; // Already cached.
+
        video::IImage *copied = driver->createImage(value->getColorFormat(),
                        value->getDimension());
        value->copyTo(copied);
@@ -90,14 +94,16 @@ video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver,
        io::path scalename = origname + "@guiScalingFilter:" + rectstr;
 
        // Search for existing scaled texture.
-       video::ITexture *scaled = g_txrCache[scalename];
+       auto it_txr = g_txrCache.find(scalename);
+       video::ITexture *scaled = (it_txr != g_txrCache.end()) ? it_txr->second : nullptr;
        if (scaled)
                return scaled;
 
        // Try to find the texture converted to an image in the cache.
        // If the image was not found, try to extract it from the texture.
-       video::IImage* srcimg = g_imgCache[origname];
-       if (srcimg == NULL) {
+       auto it_img = g_imgCache.find(origname);
+       video::IImage *srcimg = (it_img != g_imgCache.end()) ? it_img->second : nullptr;
+       if (!srcimg) {
                if (!g_settings->getBool("gui_scaling_filter_txr2img"))
                        return src;
                srcimg = driver->createImageFromData(src->getColorFormat(),