diff --git a/src/server/serverinventorymgr.cpp b/src/server/serverinventorymgr.cpp index b29bf95a5..1bdb98bd4 100644 --- a/src/server/serverinventorymgr.cpp +++ b/src/server/serverinventorymgr.cpp @@ -29,14 +29,6 @@ ServerInventoryManager::ServerInventoryManager() : InventoryManager() { } -ServerInventoryManager::~ServerInventoryManager() -{ - // Delete detached inventories - for (auto &detached_inventory : m_detached_inventories) { - delete detached_inventory.second.inventory; - } -} - Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc) { // No m_env check here: allow creation and modification of detached inventories @@ -51,7 +43,7 @@ Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc) RemotePlayer *player = m_env->getPlayer(loc.name.c_str()); if (!player) - return NULL; + return nullptr; PlayerSAO *playersao = player->getPlayerSAO(); return playersao ? playersao->getInventory() : nullptr; @@ -67,13 +59,13 @@ Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc) auto it = m_detached_inventories.find(loc.name); if (it == m_detached_inventories.end()) return nullptr; - return it->second.inventory; + return it->second.inventory.get(); } break; default: sanity_check(false); // abort break; } - return NULL; + return nullptr; } void ServerInventoryManager::setInventoryModified(const InventoryLocation &loc) @@ -113,15 +105,16 @@ Inventory *ServerInventoryManager::createDetachedInventory( if (m_detached_inventories.count(name) > 0) { infostream << "Server clearing detached inventory \"" << name << "\"" << std::endl; - delete m_detached_inventories[name].inventory; + m_detached_inventories[name].inventory.reset(); } else { infostream << "Server creating detached inventory \"" << name << "\"" << std::endl; } - Inventory *inv = new Inventory(idef); + auto inv_u = std::make_unique(idef); + auto inv = inv_u.get(); sanity_check(inv); - m_detached_inventories[name].inventory = inv; + m_detached_inventories[name].inventory = std::move(inv_u); if (!player.empty()) { m_detached_inventories[name].owner = player; @@ -152,7 +145,7 @@ bool ServerInventoryManager::removeDetachedInventory(const std::string &name) if (inv_it == m_detached_inventories.end()) return false; - delete inv_it->second.inventory; + inv_it->second.inventory.reset(); const std::string &owner = inv_it->second.owner; if (!owner.empty()) { @@ -205,6 +198,6 @@ void ServerInventoryManager::sendDetachedInventories(const std::string &peer_nam continue; } - apply_cb(detached_inventory.first, detached_inventory.second.inventory); + apply_cb(detached_inventory.first, detached_inventory.second.inventory.get()); } } diff --git a/src/server/serverinventorymgr.h b/src/server/serverinventorymgr.h index ce1af41be..b54f6edd6 100644 --- a/src/server/serverinventorymgr.h +++ b/src/server/serverinventorymgr.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "inventorymanager.h" #include #include +#include #include class IItemDefManager; @@ -31,7 +32,7 @@ class ServerInventoryManager : public InventoryManager { public: ServerInventoryManager(); - virtual ~ServerInventoryManager(); + virtual ~ServerInventoryManager() = default; void setEnv(ServerEnvironment *env) { @@ -54,7 +55,7 @@ public: private: struct DetachedInventory { - Inventory *inventory; + std::unique_ptr inventory; std::string owner; };