From a0b92764dfe91d1442f37cc09dac59a74e4b3f5b Mon Sep 17 00:00:00 2001 From: phosit Date: Fri, 16 Oct 2026 17:02:15 +0200 Subject: [PATCH] Leave g_Game uninitialized when CNetClient throws When it failed to connect to a host `g_Game` was still initialized. Retrying that crashed the program. --- .../network/scripting/JSInterface_Network.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/source/network/scripting/JSInterface_Network.cpp b/source/network/scripting/JSInterface_Network.cpp index 600e39569a..4ff5f0d3d4 100644 --- a/source/network/scripting/JSInterface_Network.cpp +++ b/source/network/scripting/JSInterface_Network.cpp @@ -113,9 +113,10 @@ void StartNetworkHost(const CStrW& playerName, const std::uint16_t serverPort, c // Generate a secret to identify the host client. - g_Game = new CGame(storeReplay); - g_NetClient = new CNetClient(g_Game, "127.0.0.1", serverPort, playerName, hostJID, hashedPassword, - secret); + auto tempGame = std::make_unique(storeReplay); + g_NetClient = new CNetClient(tempGame.get(), "127.0.0.1", serverPort, playerName, hostJID, + hashedPassword, secret); + g_Game = tempGame.release(); } void StartNetworkJoin(const CStrW& playerName, const CStr& serverAddress, std::uint16_t serverPort, @@ -125,8 +126,9 @@ void StartNetworkJoin(const CStrW& playerName, const CStr& serverAddress, std::u ENSURE(!g_NetServer); ENSURE(!g_Game); - g_Game = new CGame(storeReplay); - g_NetClient = new CNetClient(g_Game, serverAddress, serverPort, playerName); + auto tempGame = std::make_unique(storeReplay); + g_NetClient = new CNetClient(tempGame.get(), serverAddress, serverPort, playerName); + g_Game = tempGame.release(); } #if CONFIG2_LOBBY @@ -143,8 +145,9 @@ void StartNetworkJoinLobby(const CStrW& playerName, const CStr& hostJID, const C ENSURE(!g_Game); CStr hashedPass = HashCryptographically(password, hostJID + password + PS_SERIALIZATION_VERSION); - g_Game = new CGame(true); - g_NetClient = new CNetClient(g_Game, playerName, hostJID, hashedPass, *g_XmppClient); + auto tempGame = std::make_unique(true); + g_NetClient = new CNetClient(tempGame.get(), playerName, hostJID, hashedPass, *g_XmppClient); + g_Game = tempGame.release(); } #endif // CONFIG2_LOBBY