From ff862a2f206aa2449e4c7ee77d2914b091103086 Mon Sep 17 00:00:00 2001 From: Gene Walters Date: Mon, 22 Nov 2021 18:56:48 -0800 Subject: [PATCH] Fix memory leak of ProcessWatcher Signed-off-by: Gene Walters --- .../Editor/MultiplayerEditorSystemComponent.cpp | 13 +++++++++---- .../Editor/MultiplayerEditorSystemComponent.h | 6 ++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Gems/Multiplayer/Code/Source/Editor/MultiplayerEditorSystemComponent.cpp b/Gems/Multiplayer/Code/Source/Editor/MultiplayerEditorSystemComponent.cpp index 669393e31e..7cc6fbc1b5 100644 --- a/Gems/Multiplayer/Code/Source/Editor/MultiplayerEditorSystemComponent.cpp +++ b/Gems/Multiplayer/Code/Source/Editor/MultiplayerEditorSystemComponent.cpp @@ -193,7 +193,7 @@ namespace Multiplayer } } - AzFramework::ProcessWatcher* LaunchEditorServer() + void MultiplayerEditorSystemComponent::LaunchEditorServer() { // Assemble the server's path AZ::CVarFixedString serverProcess = editorsv_process; @@ -248,7 +248,13 @@ namespace Multiplayer "MultiplayerEditor", processLaunchInfo.m_launchResult != AzFramework::ProcessLauncher::ProcessLaunchResult::PLR_MissingFile, "LaunchEditorServer failed! The ServerLauncher binary is missing! (%s) Please build server launcher.", serverPath.c_str()) - return outProcess; + // Stop the previous server if one exists + if (m_serverProcessWatcher) + { + m_serverProcessWatcher->TerminateProcess(0); + } + m_serverProcessWatcher.reset(outProcess); + m_serverProcessTracePrinter = AZStd::make_unique(m_serverProcessWatcher->GetCommunicator(), "EditorServer"); } void MultiplayerEditorSystemComponent::OnGameEntitiesStarted() @@ -308,8 +314,7 @@ namespace Multiplayer editorNetworkInterface->Listen(editorsv_port); // Launch the editor-server - m_serverProcessWatcher = LaunchEditorServer(); - m_serverProcessTracePrinter = AZStd::make_unique(m_serverProcessWatcher->GetCommunicator(), "EditorServer"); + LaunchEditorServer(); AZ::TickBus::Handler::BusConnect(); } else diff --git a/Gems/Multiplayer/Code/Source/Editor/MultiplayerEditorSystemComponent.h b/Gems/Multiplayer/Code/Source/Editor/MultiplayerEditorSystemComponent.h index 330a2c9d81..5e49f8f08d 100644 --- a/Gems/Multiplayer/Code/Source/Editor/MultiplayerEditorSystemComponent.h +++ b/Gems/Multiplayer/Code/Source/Editor/MultiplayerEditorSystemComponent.h @@ -83,7 +83,9 @@ namespace Multiplayer bool IsInGameMode() override; //! @} - private: + private: + void LaunchEditorServer(); + //! EditorEvents::Handler overrides //! @{ void OnEditorNotifyEvent(EEditorNotifyEvent event) override; @@ -106,7 +108,7 @@ namespace Multiplayer //! @} IEditor* m_editor = nullptr; - AzFramework::ProcessWatcher* m_serverProcessWatcher = nullptr; + AZStd::unique_ptr m_serverProcessWatcher = nullptr; AZStd::unique_ptr m_serverProcessTracePrinter = nullptr; AzNetworking::ConnectionId m_editorConnId;