From a1a1128c880aa471d02bcd7a92ae6c88ab9411c1 Mon Sep 17 00:00:00 2001 From: mwydmuch Date: Thu, 15 Nov 2018 04:45:25 +0100 Subject: [PATCH] Hotfix for #355 issue --- src/lib/ViZDoomController.cpp | 83 +++++++++++++----------- src/lib/ViZDoomController.h | 10 ++- src/lib/ViZDoomGame.cpp | 9 ++- src/vizdoom/src/posix/sdl/crashcatcher.c | 8 ++- 4 files changed, 66 insertions(+), 44 deletions(-) diff --git a/src/lib/ViZDoomController.cpp b/src/lib/ViZDoomController.cpp index 4b7906806..c2ba7685c 100644 --- a/src/lib/ViZDoomController.cpp +++ b/src/lib/ViZDoomController.cpp @@ -25,7 +25,6 @@ #include "ViZDoomPathHelpers.h" #include "ViZDoomUtilities.h" #include "ViZDoomVersion.h" -#include "boost/process.hpp" #include #include @@ -37,8 +36,6 @@ namespace vizdoom { namespace bal = boost::algorithm; namespace bc = boost::chrono; namespace bfs = boost::filesystem; - namespace bpr = boost::process; - namespace bpri = boost::process::initializers; /* Public methods */ @@ -198,47 +195,55 @@ namespace vizdoom { } void DoomController::close() { + try{ + if (this->doomRunning) { + this->doomRunning = false; + this->doomWorking = false; + + this->MQDoom->send(MSG_CODE_CLOSE); + + #ifdef OS_LINUX + if(0 == kill(this->doomProcessPid, 0)){ + bpr::child doomProcess(this->doomProcessPid); + bpr::terminate(doomProcess); + } + #endif + } - if (this->doomRunning) { - - this->doomRunning = false; - this->doomWorking = false; - - this->MQDoom->send(MSG_CODE_CLOSE); - } - - if (this->signalThread && this->signalThread->joinable()) { - this->ioService->stop(); + if (this->signalThread && this->signalThread->joinable()) { + this->ioService->stop(); - this->signalThread->interrupt(); - this->signalThread->join(); - delete this->signalThread; - this->signalThread = nullptr; + this->signalThread->interrupt(); + this->signalThread->join(); + delete this->signalThread; + this->signalThread = nullptr; - delete this->ioService; - this->ioService = nullptr; - } + delete this->ioService; + this->ioService = nullptr; + } - if (this->doomThread && this->doomThread->joinable()) { - this->doomThread->interrupt(); - this->doomThread->join(); - delete this->doomThread; - this->doomThread = nullptr; - } + if (this->doomThread && this->doomThread->joinable()) { + this->doomThread->interrupt(); + this->doomThread->join(); + delete this->doomThread; + this->doomThread = nullptr; + } - if (this->SM) { - delete this->SM; - this->SM = nullptr; - } + if (this->SM) { + delete this->SM; + this->SM = nullptr; + } - if (this->MQDoom) { - delete this->MQDoom; - this->MQDoom = nullptr; - } - if (this->MQController) { - delete this->MQController; - this->MQController = nullptr; + if (this->MQDoom) { + delete this->MQDoom; + this->MQDoom = nullptr; + } + if (this->MQController) { + delete this->MQController; + this->MQController = nullptr; + } } + catch (...) { throw; } this->gameState = nullptr; this->input = nullptr; @@ -246,6 +251,7 @@ namespace vizdoom { this->depthBuffer = nullptr; this->labelsBuffer = nullptr; this->automapBuffer = nullptr; + } void DoomController::restart() { @@ -1338,6 +1344,9 @@ namespace vizdoom { void DoomController::launchDoom() { try { bpr::child doomProcess = bpr::execute(bpri::set_args(this->doomArgs), bpri::inherit_env()); + #ifdef OS_LINUX + this->doomProcessPid = doomProcess.pid; + #endif bpr::wait_for_exit(doomProcess); } catch (...) { diff --git a/src/lib/ViZDoomController.h b/src/lib/ViZDoomController.h index aeabebfa7..d58210ed2 100644 --- a/src/lib/ViZDoomController.h +++ b/src/lib/ViZDoomController.h @@ -26,6 +26,7 @@ #include "ViZDoomTypes.h" #include "ViZDoomMessageQueue.h" #include "ViZDoomSharedMemory.h" +#include "boost/process.hpp" #include #include @@ -40,6 +41,9 @@ namespace vizdoom { namespace bip = boost::interprocess; namespace br = boost::random; namespace bs = boost::system; + namespace bpr = boost::process; + namespace bpri = boost::process::initializers; + #define INSTANCE_ID_LENGTH 10 @@ -70,6 +74,8 @@ namespace vizdoom { /* OSes */ #ifdef __linux__ #define OS_LINUX + #include + #include #elif _WIN32 #define OS_WIN #elif __APPLE__ @@ -292,8 +298,10 @@ namespace vizdoom { void intSignal(int sigNumber); b::thread *doomThread; - //bpr::child doomProcess; + #ifdef OS_LINUX + pid_t doomProcessPid; + #endif /* Message queues */ /*------------------------------------------------------------------------------------------------------------*/ diff --git a/src/lib/ViZDoomGame.cpp b/src/lib/ViZDoomGame.cpp index e2006cece..54d176479 100644 --- a/src/lib/ViZDoomGame.cpp +++ b/src/lib/ViZDoomGame.cpp @@ -97,7 +97,11 @@ namespace vizdoom { void DoomGame::close() { if (this->isRunning()) { - this->doomController->close(); + try { + this->doomController->close(); + } + catch (...) { throw; } + this->lastAction.clear(); this->nextAction.clear(); @@ -162,10 +166,9 @@ namespace vizdoom { if (this->doomController->isTicPossible()) { try { this->doomController->tics(tics, updateState); + if (updateState) this->updateState(); } catch (...) { throw; } - - if (updateState) this->updateState(); } } diff --git a/src/vizdoom/src/posix/sdl/crashcatcher.c b/src/vizdoom/src/posix/sdl/crashcatcher.c index 70c9e7b0c..3b691651c 100644 --- a/src/vizdoom/src/posix/sdl/crashcatcher.c +++ b/src/vizdoom/src/posix/sdl/crashcatcher.c @@ -216,11 +216,13 @@ static void crash_catcher(int signum, siginfo_t *siginfo, void *context) crash_info.has_siginfo = !!siginfo; if(siginfo) crash_info.siginfo = *siginfo; - if(cc_user_info) - cc_user_info(crash_info.buf, crash_info.buf+sizeof(crash_info.buf)); + + //VIZDOOM_CODE +// if(cc_user_info) +// cc_user_info(crash_info.buf, crash_info.buf+sizeof(crash_info.buf)); /* Fork off to start a crash handler */ - switch((dbg_pid=fork())) + switch((dbg_pid=fork())) { /* Error */ case -1: