Skip to content

Commit

Permalink
Server: properly delete ServerMap on interrupted startups
Browse files Browse the repository at this point in the history
A static mod error (e.g. typo) would abort the initialization but never free ServerMap
  • Loading branch information
SmallJoker committed Feb 5, 2021
1 parent 9b64834 commit d287da1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/map_settings_manager.cpp
Expand Up @@ -116,7 +116,8 @@ bool MapSettingsManager::saveMapMeta()
{
// If mapgen params haven't been created yet; abort
if (!mapgen_params) {
errorstream << "saveMapMeta: mapgen_params not present!" << std::endl;
infostream << "saveMapMeta: mapgen_params not present! "
<< "Server startup was probably interrupted." << std::endl;
return false;
}

Expand Down
3 changes: 3 additions & 0 deletions src/server.cpp
Expand Up @@ -351,6 +351,7 @@ Server::~Server()
// Deinitialize scripting
infostream << "Server: Deinitializing scripting" << std::endl;
delete m_script;
delete m_startup_server_map; // if available
delete m_game_settings;

while (!m_unsent_map_edit_queue.empty()) {
Expand Down Expand Up @@ -399,6 +400,7 @@ void Server::init()

// Create the Map (loads map_meta.txt, overriding configured mapgen params)
ServerMap *servermap = new ServerMap(m_path_world, this, m_emerge, m_metrics_backend.get());
m_startup_server_map = servermap;

// Initialize scripting
infostream << "Server: Initializing Lua" << std::endl;
Expand Down Expand Up @@ -440,6 +442,7 @@ void Server::init()
m_craftdef->initHashes(this);

// Initialize Environment
m_startup_server_map = nullptr; // Ownership moved to ServerEnvironment
m_env = new ServerEnvironment(servermap, m_script, this, m_path_world);

m_inventory_mgr->setEnv(m_env);
Expand Down
4 changes: 4 additions & 0 deletions src/server.h
Expand Up @@ -547,6 +547,10 @@ class Server : public con::PeerHandler, public MapEventReceiver,
// Environment
ServerEnvironment *m_env = nullptr;

// Reference to the server map until ServerEnvironment is initialized
// after that this variable must be a nullptr
ServerMap *m_startup_server_map = nullptr;

// server connection
std::shared_ptr<con::Connection> m_con;

Expand Down

0 comments on commit d287da1

Please sign in to comment.