Fix some reference counters (memleak) (#8981)
authorSmallJoker <SmallJoker@users.noreply.github.com>
Tue, 24 Sep 2019 17:05:28 +0000 (19:05 +0200)
committerGitHub <noreply@github.com>
Tue, 24 Sep 2019 17:05:28 +0000 (19:05 +0200)
Fix some reference counters (memleak)
Map::dispatchEvent: Allocation safety using references

12 files changed:
src/client/fontengine.cpp
src/gui/guiFormSpecMenu.cpp
src/map.cpp
src/map.h
src/mapgen/mg_schematic.cpp
src/mapgen/treegen.cpp
src/rollback_interface.cpp
src/script/lua_api/l_env.cpp
src/script/lua_api/l_nodemeta.cpp
src/script/lua_api/l_vmanip.cpp
src/server.cpp
src/server.h

index c32d0f1a48b6b494edbe6f6893132cbfba0589a8..858d6780e9773849a80c16ccb20a2ebebb346964 100644 (file)
@@ -275,7 +275,6 @@ void FontEngine::initFont(unsigned int basesize, FontMode mode)
                                font_shadow_alpha);
 
                if (font) {
-                       font->grab();
                        m_font_cache[mode][basesize] = font;
                        return;
                }
@@ -365,8 +364,6 @@ void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
                }
        }
 
-       if (font) {
-               font->grab();
+       if (font)
                m_font_cache[mode][basesize] = font;
-       }
 }
index e8a7f546ea7e612fd0c3db74660334794eb3a018..390c81bc168f94c3e47283b3c48efd56992979e0 100644 (file)
@@ -1208,12 +1208,14 @@ void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
                        true, Environment, this, spec.fid, rect, is_editable, is_multiline);
                e->drop();
        } else {
-               if (is_multiline)
+               if (is_multiline) {
                        e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true,
                                Environment, this, spec.fid, rect, is_editable, true);
-               else if (is_editable)
+                       e->drop();
+               } else if (is_editable) {
                        e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
                                this, spec.fid);
+               }
        }
 
        if (e) {
index 022eb9f198290190a159a57af7c095f9fd7c686f..0a7099a06e5a8574016837cd85966ba0683e5de8 100644 (file)
@@ -89,7 +89,7 @@ void Map::removeEventReceiver(MapEventReceiver *event_receiver)
        m_event_receivers.erase(event_receiver);
 }
 
-void Map::dispatchEvent(MapEditEvent *event)
+void Map::dispatchEvent(const MapEditEvent &event)
 {
        for (MapEventReceiver *event_receiver : m_event_receivers) {
                event_receiver->onMapEditEvent(event);
@@ -274,7 +274,7 @@ bool Map::addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata)
                succeeded = false;
        }
 
-       dispatchEvent(&event);
+       dispatchEvent(event);
 
        return succeeded;
 }
@@ -299,7 +299,7 @@ bool Map::removeNodeWithEvent(v3s16 p)
                succeeded = false;
        }
 
-       dispatchEvent(&event);
+       dispatchEvent(event);
 
        return succeeded;
 }
@@ -2220,7 +2220,7 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos)
                        for (it = modified_blocks.begin();
                                        it != modified_blocks.end(); ++it)
                                event.modified_blocks.insert(it->first);
-                       dispatchEvent(&event);
+                       dispatchEvent(event);
                }
        }
        return block;
index 67e00c6f4d1943066c6fd6ac06a78c0e65ce0088..392ec3f256a02ee49eaf872825d645a3a4585bd0 100644 (file)
--- a/src/map.h
+++ b/src/map.h
@@ -79,18 +79,7 @@ struct MapEditEvent
 
        MapEditEvent() = default;
 
-       MapEditEvent * clone()
-       {
-               MapEditEvent *event = new MapEditEvent();
-               event->type = type;
-               event->p = p;
-               event->n = n;
-               event->modified_blocks = modified_blocks;
-               event->is_private_change = is_private_change;
-               return event;
-       }
-
-       VoxelArea getArea()
+       VoxelArea getArea() const
        {
                switch(type){
                case MEET_ADDNODE:
@@ -125,7 +114,7 @@ class MapEventReceiver
 {
 public:
        // event shall be deleted by caller after the call.
-       virtual void onMapEditEvent(MapEditEvent *event) = 0;
+       virtual void onMapEditEvent(const MapEditEvent &event) = 0;
 };
 
 class Map /*: public NodeContainer*/
@@ -152,7 +141,7 @@ public:
        void addEventReceiver(MapEventReceiver *event_receiver);
        void removeEventReceiver(MapEventReceiver *event_receiver);
        // event shall be deleted by caller after the call.
-       void dispatchEvent(MapEditEvent *event);
+       void dispatchEvent(const MapEditEvent &event);
 
        // On failure returns NULL
        MapSector * getSectorNoGenerateNoLock(v2s16 p2d);
index 36f1dd76b3b6fe5aa63232aab3ac21c47d0b3d14..c1acbfd9d6f370fab4b1b5c5612402a2881823a8 100644 (file)
@@ -246,7 +246,7 @@ void Schematic::placeOnMap(ServerMap *map, v3s16 p, u32 flags,
        for (it = modified_blocks.begin(); it != modified_blocks.end(); ++it)
                event.modified_blocks.insert(it->first);
 
-       map->dispatchEvent(&event);
+       map->dispatchEvent(event);
 }
 
 
index 4c351fcefae0ef087ced97e67f3e04b64f72f2a1..0d8af2851ed5592d04e87b69929ea7ea14ad421e 100644 (file)
@@ -135,7 +135,7 @@ treegen::error spawn_ltree(ServerEnvironment *env, v3s16 p0,
        event.type = MEET_OTHER;
        for (auto &modified_block : modified_blocks)
                event.modified_blocks.insert(modified_block.first);
-       map->dispatchEvent(&event);
+       map->dispatchEvent(event);
        return SUCCESS;
 }
 
index 0c8aef9ab5137afabe81c6474a730f6dddc00f2e..c00206e984592a53f167ced313544b672dbbbcdc 100644 (file)
@@ -176,7 +176,7 @@ bool RollbackAction::applyRevert(Map *map, InventoryManager *imgr, IGameDef *gam
                                MapEditEvent event;
                                event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
                                event.p = p;
-                               map->dispatchEvent(&event);
+                               map->dispatchEvent(event);
                        } catch (InvalidPositionException &e) {
                                infostream << "RollbackAction::applyRevert(): "
                                        << "InvalidPositionException: " << e.what()
index 34e079af72df945e78389b42baaeef44a02b154c..a56b1cb0b120f845af670acd442ad26aeaad6556 100644 (file)
@@ -1035,7 +1035,7 @@ int ModApiEnvMod::l_fix_light(lua_State *L)
                for (auto &modified_block : modified_blocks)
                        event.modified_blocks.insert(modified_block.first);
 
-               map.dispatchEvent(&event);
+               map.dispatchEvent(event);
        }
        lua_pushboolean(L, success);
 
@@ -1144,7 +1144,7 @@ int ModApiEnvMod::l_delete_area(lua_State *L)
                }
        }
 
-       map.dispatchEvent(&event);
+       map.dispatchEvent(event);
        lua_pushboolean(L, success);
        return 1;
 }
index 22fc617829e1cb2abf2d4823ac565cc725a4b6eb..229ce73dbc044cc27078ec40451b70405e5e2c33 100644 (file)
@@ -68,7 +68,7 @@ void NodeMetaRef::reportMetadataChange(const std::string *name)
        event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
        event.p = m_p;
        event.is_private_change = name && meta && meta->isPrivate(*name);
-       m_env->getMap().dispatchEvent(&event);
+       m_env->getMap().dispatchEvent(event);
 }
 
 // Exported functions
index c92983bd3d332dcca373a0781fcecf48e83f4378..fd73d21d1876fa81c9b7b546bc17b9f8ec432f68 100644 (file)
@@ -126,7 +126,7 @@ int LuaVoxelManip::l_write_to_map(lua_State *L)
        for (const auto &modified_block : o->modified_blocks)
                event.modified_blocks.insert(modified_block.first);
 
-       map->dispatchEvent(&event);
+       map->dispatchEvent(event);
 
        o->modified_blocks.clear();
        return 0;
index c0ed02b9f1cbedc92791d4b54140fb5da68c202c..0e677199aa1d517a31078ec1c7513b39cf33865b 100644 (file)
@@ -306,6 +306,11 @@ Server::~Server()
        for (auto &detached_inventory : m_detached_inventories) {
                delete detached_inventory.second;
        }
+
+       while (!m_unsent_map_edit_queue.empty()) {
+               delete m_unsent_map_edit_queue.front();
+               m_unsent_map_edit_queue.pop();
+       }
 }
 
 void Server::init()
@@ -1090,12 +1095,12 @@ void Server::setTimeOfDay(u32 time)
        m_time_of_day_send_timer = 0;
 }
 
-void Server::onMapEditEvent(MapEditEvent *event)
+void Server::onMapEditEvent(const MapEditEvent &event)
 {
-       if (m_ignore_map_edit_events_area.contains(event->getArea()))
+       if (m_ignore_map_edit_events_area.contains(event.getArea()))
                return;
-       MapEditEvent *e = event->clone();
-       m_unsent_map_edit_queue.push(e);
+
+       m_unsent_map_edit_queue.push(new MapEditEvent(event));
 }
 
 Inventory* Server::getInventory(const InventoryLocation &loc)
@@ -1160,7 +1165,7 @@ void Server::setInventoryModified(const InventoryLocation &loc)
                MapEditEvent event;
                event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
                event.p = loc.p;
-               m_env->getMap().dispatchEvent(&event);
+               m_env->getMap().dispatchEvent(event);
        }
                break;
        case InventoryLocation::DETACHED:
index f017331799515a1cc1ca2f53dffb43d4085cac31..d61840871b933d6afa9b1cf84f45d4be52f58909 100644 (file)
@@ -189,7 +189,7 @@ public:
                This is accessed by the map, which is inside the environment,
                so it shouldn't be a problem.
        */
-       void onMapEditEvent(MapEditEvent *event);
+       void onMapEditEvent(const MapEditEvent &event);
 
        /*
                Shall be called with the environment and the connection locked.