Fix some reference counters (memleak) (#8981)

Fix some reference counters (memleak)
Map::dispatchEvent: Allocation safety using references
This commit is contained in:
SmallJoker 2019-09-24 19:05:28 +02:00 committed by GitHub
parent d77ea76476
commit 26b39f1eae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 30 additions and 37 deletions

@ -275,7 +275,6 @@ void FontEngine::initFont(unsigned int basesize, FontMode mode)
font_shadow_alpha); font_shadow_alpha);
if (font) { if (font) {
font->grab();
m_font_cache[mode][basesize] = font; m_font_cache[mode][basesize] = font;
return; return;
} }
@ -365,8 +364,6 @@ void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
} }
} }
if (font) { if (font)
font->grab();
m_font_cache[mode][basesize] = font; m_font_cache[mode][basesize] = font;
}
} }

@ -1208,12 +1208,14 @@ void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
true, Environment, this, spec.fid, rect, is_editable, is_multiline); true, Environment, this, spec.fid, rect, is_editable, is_multiline);
e->drop(); e->drop();
} else { } else {
if (is_multiline) if (is_multiline) {
e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true, e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true,
Environment, this, spec.fid, rect, is_editable, 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, e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
this, spec.fid); this, spec.fid);
}
} }
if (e) { if (e) {

@ -89,7 +89,7 @@ void Map::removeEventReceiver(MapEventReceiver *event_receiver)
m_event_receivers.erase(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) { for (MapEventReceiver *event_receiver : m_event_receivers) {
event_receiver->onMapEditEvent(event); event_receiver->onMapEditEvent(event);
@ -274,7 +274,7 @@ bool Map::addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata)
succeeded = false; succeeded = false;
} }
dispatchEvent(&event); dispatchEvent(event);
return succeeded; return succeeded;
} }
@ -299,7 +299,7 @@ bool Map::removeNodeWithEvent(v3s16 p)
succeeded = false; succeeded = false;
} }
dispatchEvent(&event); dispatchEvent(event);
return succeeded; return succeeded;
} }
@ -2220,7 +2220,7 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos)
for (it = modified_blocks.begin(); for (it = modified_blocks.begin();
it != modified_blocks.end(); ++it) it != modified_blocks.end(); ++it)
event.modified_blocks.insert(it->first); event.modified_blocks.insert(it->first);
dispatchEvent(&event); dispatchEvent(event);
} }
} }
return block; return block;

@ -79,18 +79,7 @@ struct MapEditEvent
MapEditEvent() = default; MapEditEvent() = default;
MapEditEvent * clone() VoxelArea getArea() const
{
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()
{ {
switch(type){ switch(type){
case MEET_ADDNODE: case MEET_ADDNODE:
@ -125,7 +114,7 @@ class MapEventReceiver
{ {
public: public:
// event shall be deleted by caller after the call. // 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*/ class Map /*: public NodeContainer*/
@ -152,7 +141,7 @@ public:
void addEventReceiver(MapEventReceiver *event_receiver); void addEventReceiver(MapEventReceiver *event_receiver);
void removeEventReceiver(MapEventReceiver *event_receiver); void removeEventReceiver(MapEventReceiver *event_receiver);
// event shall be deleted by caller after the call. // event shall be deleted by caller after the call.
void dispatchEvent(MapEditEvent *event); void dispatchEvent(const MapEditEvent &event);
// On failure returns NULL // On failure returns NULL
MapSector * getSectorNoGenerateNoLock(v2s16 p2d); MapSector * getSectorNoGenerateNoLock(v2s16 p2d);

@ -246,7 +246,7 @@ void Schematic::placeOnMap(ServerMap *map, v3s16 p, u32 flags,
for (it = modified_blocks.begin(); it != modified_blocks.end(); ++it) for (it = modified_blocks.begin(); it != modified_blocks.end(); ++it)
event.modified_blocks.insert(it->first); event.modified_blocks.insert(it->first);
map->dispatchEvent(&event); map->dispatchEvent(event);
} }

@ -135,7 +135,7 @@ treegen::error spawn_ltree(ServerEnvironment *env, v3s16 p0,
event.type = MEET_OTHER; event.type = MEET_OTHER;
for (auto &modified_block : modified_blocks) for (auto &modified_block : modified_blocks)
event.modified_blocks.insert(modified_block.first); event.modified_blocks.insert(modified_block.first);
map->dispatchEvent(&event); map->dispatchEvent(event);
return SUCCESS; return SUCCESS;
} }

@ -176,7 +176,7 @@ bool RollbackAction::applyRevert(Map *map, InventoryManager *imgr, IGameDef *gam
MapEditEvent event; MapEditEvent event;
event.type = MEET_BLOCK_NODE_METADATA_CHANGED; event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
event.p = p; event.p = p;
map->dispatchEvent(&event); map->dispatchEvent(event);
} catch (InvalidPositionException &e) { } catch (InvalidPositionException &e) {
infostream << "RollbackAction::applyRevert(): " infostream << "RollbackAction::applyRevert(): "
<< "InvalidPositionException: " << e.what() << "InvalidPositionException: " << e.what()

@ -1035,7 +1035,7 @@ int ModApiEnvMod::l_fix_light(lua_State *L)
for (auto &modified_block : modified_blocks) for (auto &modified_block : modified_blocks)
event.modified_blocks.insert(modified_block.first); event.modified_blocks.insert(modified_block.first);
map.dispatchEvent(&event); map.dispatchEvent(event);
} }
lua_pushboolean(L, success); 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); lua_pushboolean(L, success);
return 1; return 1;
} }

@ -68,7 +68,7 @@ void NodeMetaRef::reportMetadataChange(const std::string *name)
event.type = MEET_BLOCK_NODE_METADATA_CHANGED; event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
event.p = m_p; event.p = m_p;
event.is_private_change = name && meta && meta->isPrivate(*name); event.is_private_change = name && meta && meta->isPrivate(*name);
m_env->getMap().dispatchEvent(&event); m_env->getMap().dispatchEvent(event);
} }
// Exported functions // Exported functions

@ -126,7 +126,7 @@ int LuaVoxelManip::l_write_to_map(lua_State *L)
for (const auto &modified_block : o->modified_blocks) for (const auto &modified_block : o->modified_blocks)
event.modified_blocks.insert(modified_block.first); event.modified_blocks.insert(modified_block.first);
map->dispatchEvent(&event); map->dispatchEvent(event);
o->modified_blocks.clear(); o->modified_blocks.clear();
return 0; return 0;

@ -306,6 +306,11 @@ Server::~Server()
for (auto &detached_inventory : m_detached_inventories) { for (auto &detached_inventory : m_detached_inventories) {
delete detached_inventory.second; 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() void Server::init()
@ -1090,12 +1095,12 @@ void Server::setTimeOfDay(u32 time)
m_time_of_day_send_timer = 0; 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; 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) Inventory* Server::getInventory(const InventoryLocation &loc)
@ -1160,7 +1165,7 @@ void Server::setInventoryModified(const InventoryLocation &loc)
MapEditEvent event; MapEditEvent event;
event.type = MEET_BLOCK_NODE_METADATA_CHANGED; event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
event.p = loc.p; event.p = loc.p;
m_env->getMap().dispatchEvent(&event); m_env->getMap().dispatchEvent(event);
} }
break; break;
case InventoryLocation::DETACHED: case InventoryLocation::DETACHED:

@ -189,7 +189,7 @@ public:
This is accessed by the map, which is inside the environment, This is accessed by the map, which is inside the environment,
so it shouldn't be a problem. 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. Shall be called with the environment and the connection locked.