Skip to content

Commit 4f8a847

Browse files
authored
Guard new object from being added at shutdown (#16610)
1 parent f1c0f29 commit 4f8a847

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/serverenvironment.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,20 @@ void ServerEnvironment::init()
278278

279279
void ServerEnvironment::deactivateBlocksAndObjects()
280280
{
281+
// Prevent any funny business from happening in case further callbacks
282+
// try to add new objects.
283+
m_shutting_down = true;
284+
281285
// Clear active block list.
282-
// This makes the next one delete all active objects.
286+
// This makes the next code delete all active objects.
283287
m_active_blocks.clear();
284288

285289
deactivateFarObjects(true);
286290
}
287291

288292
ServerEnvironment::~ServerEnvironment()
289293
{
294+
m_script = nullptr;
290295
assert(m_active_blocks.size() == 0); // deactivateBlocksAndObjects does this
291296

292297
// Drop/delete map
@@ -301,9 +306,12 @@ ServerEnvironment::~ServerEnvironment()
301306
for (RemotePlayer *m_player : m_players) {
302307
delete m_player;
303308
}
309+
m_players.clear();
304310

305311
delete m_player_database;
312+
m_player_database = nullptr;
306313
delete m_auth_database;
314+
m_auth_database = nullptr;
307315
}
308316

309317
Map & ServerEnvironment::getMap()
@@ -1208,6 +1216,11 @@ void ServerEnvironment::deleteParticleSpawner(u32 id, bool remove_from_object)
12081216
u16 ServerEnvironment::addActiveObject(std::unique_ptr<ServerActiveObject> object)
12091217
{
12101218
assert(object); // Pre-condition
1219+
if (m_shutting_down) {
1220+
warningstream << "ServerEnvironment: refusing to add active object "
1221+
"during shutdown: " << object->getDescription() << std::endl;
1222+
return 0;
1223+
}
12111224
m_added_objects++;
12121225
u16 id = addActiveObjectRaw(std::move(object), nullptr, 0);
12131226
return id;
@@ -1569,7 +1582,7 @@ std::unique_ptr<ServerActiveObject> ServerEnvironment::createSAO(ActiveObjectTyp
15691582
*/
15701583
void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
15711584
{
1572-
if (block == NULL)
1585+
if (!block || m_shutting_down)
15731586
return;
15741587

15751588
if (!block->onObjectsActivation())

src/serverenvironment.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ class ServerEnvironment final : public Environment
357357
// The map
358358
std::unique_ptr<ServerMap> m_map;
359359
// Lua state
360-
ServerScripting* m_script;
360+
ServerScripting *m_script = nullptr;
361361
// Server definition
362-
Server *m_server;
362+
Server *m_server = nullptr;
363363
// Active Object Manager
364364
server::ActiveObjectMgr m_ao_manager;
365365
// on_mapblocks_changed map event receiver
@@ -378,6 +378,8 @@ class ServerEnvironment final : public Environment
378378
IntervalLimiter m_active_blocks_nodemetadata_interval;
379379
// Whether the variables below have been read from file yet
380380
bool m_meta_loaded = false;
381+
// Are we shutting down?
382+
bool m_shutting_down = false;
381383
// Time from the beginning of the game in seconds.
382384
// Incremented in step().
383385
u32 m_game_time = 0;

0 commit comments

Comments
 (0)