diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index 5bea65d1b3..91a1d7d8e4 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -2113,14 +2113,17 @@ void ScriptEngine::UpdateSockets() void ScriptEngine::RemoveSockets(const std::shared_ptr& plugin) { #ifndef DISABLE_NETWORK - std::erase_if(_sockets, [plugin](const std::shared_ptr& data) { - if (data->_plugin == plugin) - { - data->Dispose(); - return true; - } - return false; - }); + // Remove sockets from the _plugins vector by first moving them, then clearing the vector + std::vector> removed; + for (auto& data : _sockets) + { + if (data != nullptr && data->_plugin == plugin) + removed.push_back(std::move(data)); + } + std::erase_if(_sockets, [](const std::shared_ptr& data) { return data == nullptr; }); + // Dispose of the moved sockets safely + for (const auto& data : removed) + data->Dispose(); #endif }