Fixed minimap memory leak
authorBřetislav Štec <valsiterb@gmail.com>
Sat, 18 Jul 2015 23:35:47 +0000 (01:35 +0200)
committerkwolekr <kwolekr@minetest.net>
Mon, 27 Jul 2015 15:06:46 +0000 (11:06 -0400)
src/client.cpp
src/mapblock_mesh.cpp
src/mapblock_mesh.h
src/minimap.cpp

index 63f720f0404edaeb01c8b08af0841a5fb85c2aae..fdabfa2d8fc44f2b1b141b2f82bac58f3e4747f1 100644 (file)
@@ -540,20 +540,19 @@ void Client::step(float dtime)
                                }
 
                                if (r.mesh) {
-                                       minimap_mapblock = r.mesh->getMinimapMapblock();
-                                       do_mapper_update = (minimap_mapblock != NULL);
+                                       minimap_mapblock = r.mesh->moveMinimapMapblock();
+                                       if (minimap_mapblock == NULL)
+                                               do_mapper_update = false;
                                }
 
                                if (r.mesh && r.mesh->getMesh()->getMeshBufferCount() == 0) {
                                        delete r.mesh;
-                                       block->mesh = NULL;
                                } else {
                                        // Replace with the new mesh
                                        block->mesh = r.mesh;
                                }
                        } else {
                                delete r.mesh;
-                               minimap_mapblock = NULL;
                        }
 
                        if (do_mapper_update)
index 32f6e659a4957fc4d6f34ea064a0dd5384b4a679..33597b2fc18454e0a2eb9e5db789c0deaa5248d7 100644 (file)
@@ -1278,6 +1278,7 @@ MapBlockMesh::~MapBlockMesh()
 {
        m_mesh->drop();
        m_mesh = NULL;
+       delete m_minimap_mapblock;
 }
 
 bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_ratio)
index 283006331228fd5d3e0c4140ea17ac87d285b3fe..8e994ec6b76fe5db756bb07dbf6c9ad3cbef36f5 100644 (file)
@@ -104,14 +104,16 @@ public:
        // Returns true if anything has been changed.
        bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
 
-       scene::SMeshgetMesh()
+       scene::SMesh *getMesh()
        {
                return m_mesh;
        }
 
-       MinimapMapblock* getMinimapMapblock()
+       MinimapMapblock *moveMinimapMapblock()
        {
-               return m_minimap_mapblock;
+               MinimapMapblock *p = m_minimap_mapblock;
+               m_minimap_mapblock = NULL;
+               return p;
        }
 
        bool isAnimationForced() const
index d37a083efd18307bd1750c3a2ad087b9cc89a6c3..950202c6a4f8895bce0cc8ed16b6779d0ac31126 100644 (file)
@@ -102,7 +102,13 @@ void MinimapUpdateThread::doUpdate()
 
        while (popBlockUpdate(&update)) {
                if (update.data) {
-                       m_blocks_cache[update.pos] = update.data;
+                       // Swap two values in the map using single lookup
+                       std::pair<std::map<v3s16, MinimapMapblock*>::iterator, bool>
+                           result = m_blocks_cache.insert(std::make_pair(update.pos, update.data));
+                       if (result.second == false) {
+                               delete result.first->second;
+                               result.first->second = update.data;
+                       }
                } else {
                        std::map<v3s16, MinimapMapblock *>::iterator it;
                        it = m_blocks_cache.find(update.pos);