From fa1dc98103344ad23d030885be34d0e6d2ab832a Mon Sep 17 00:00:00 2001 From: phosit Date: Sun, 23 Jun 2024 17:26:03 +0000 Subject: [PATCH] Shutdown mongoose in rl-interface gracefully When the `RL::Interface` is destroied but the engine isn't completely shoutdown there was a dangling pointer. The time the dangling pointer is accessable got expanded with 968e55704a. Whith this patch there no dangling pointer (in that regard). Accepted by: @vladislavbelov Comments by: @Stan, @sera Trac Tickets: #5989 Differential Revision: https://code.wildfiregames.com/D5254 This was SVN commit r28123. --- source/rlinterface/RLInterface.cpp | 13 +++++++++---- source/rlinterface/RLInterface.h | 7 +++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/source/rlinterface/RLInterface.cpp b/source/rlinterface/RLInterface.cpp index bd449c9a75..2a7f7a1510 100644 --- a/source/rlinterface/RLInterface.cpp +++ b/source/rlinterface/RLInterface.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2023 Wildfire Games. +/* Copyright (C) 2024 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -40,7 +40,7 @@ namespace RL { -Interface::Interface(const char* server_address) : m_GameMessage({GameMessageType::None}) +Interface::Interface(const char* server_address) { LOGMESSAGERENDER("Starting RL interface HTTP server"); @@ -49,8 +49,13 @@ Interface::Interface(const char* server_address) : m_GameMessage({GameMessageTyp "num_threads", "1", nullptr }; - mg_context* mgContext = mg_start(MgCallback, this, options); - ENSURE(mgContext); + m_Context = mg_start(MgCallback, this, options); + ENSURE(m_Context); +} + +Interface::~Interface() +{ + mg_stop(m_Context); } // Interactions with the game engine (g_Game) must be done in the main diff --git a/source/rlinterface/RLInterface.h b/source/rlinterface/RLInterface.h index adbc836a12..ef93885d99 100644 --- a/source/rlinterface/RLInterface.h +++ b/source/rlinterface/RLInterface.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2023 Wildfire Games. +/* Copyright (C) 2024 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -84,6 +84,7 @@ class Interface NONCOPYABLE(Interface); public: Interface(const char* server_address); + ~Interface(); /** * Non-blocking call to process any pending messages from the RL client. @@ -149,7 +150,7 @@ private: std::string GetGameState() const; private: - GameMessage m_GameMessage; + GameMessage m_GameMessage{GameMessageType::None}; ScenarioConfig m_ScenarioConfig; std::string m_ReturnValue; bool m_NeedsGameState = false; @@ -158,6 +159,8 @@ private: std::mutex m_MsgLock; std::condition_variable m_MsgApplied; std::string m_Code; + + mg_context* m_Context; }; }