Skip to content

Commit

Permalink
guiScalingFilter: Fix most memory leaks (minetest#12256)
Browse files Browse the repository at this point in the history
Calls to the cache function ended up creating a new texture regardless whether
the texture is already cached.
  • Loading branch information
SmallJoker committed May 4, 2022
1 parent 0704ca0 commit 3ce5a68
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ Client::~Client()
// cleanup 3d model meshes on client shutdown
m_rendering_engine->cleanupMeshCache();

guiScalingCacheClear();

delete m_minimap;
m_minimap = nullptr;

Expand Down
12 changes: 9 additions & 3 deletions src/client/guiscalingfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 3ce5a68

Please sign in to comment.