From ce6af3d6498c92f4c1ab264cfb376d8e7093ce06 Mon Sep 17 00:00:00 2001 From: Pauli Date: Tue, 10 Jul 2018 18:21:05 +0300 Subject: [PATCH 1/4] Use c++ standard std::_Exit instead of _exit _exit seems to run dll unloading code which calls static destructors. Standrd requires std::_Exit not to call destructors which makes using it attractive in case MSVC actually follows the standard. --- library/Core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Core.cpp b/library/Core.cpp index 479eaea08..8dcfa3d8c 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1163,7 +1163,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, v } else if (builtin == "die") { - _exit(666); + std::_Exit(666); } else if (builtin == "kill-lua") { From 5eea7198a9c846aed502704641844145cccbed4c Mon Sep 17 00:00:00 2001 From: Pauli Date: Tue, 10 Jul 2018 18:23:22 +0300 Subject: [PATCH 2/4] Make Core::~Core safe to call after die Fixes #1356 --- library/Core.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index 8dcfa3d8c..87afdf2c9 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1479,17 +1479,7 @@ void fIOthread(void * iodata) Core::~Core() { - if (MainThread::suspend().owns_lock()) - MainThread::suspend().unlock(); - - if (d->hotkeythread.joinable()) { - std::lock_guard lock(HotkeyMutex); - hotkey_set = SHUTDOWN; - HotkeyCond.notify_one(); - } - if (d->iothread.joinable()) - con.shutdown(); - delete d; + // we leak the memory in case ~Core is called after _exit } Core::Core() : @@ -2299,6 +2289,8 @@ int Core::Shutdown ( void ) } allModules.clear(); memset(&(s_mods), 0, sizeof(s_mods)); + delete d; + d = nullptr; return -1; } From 7d92d3f48fd5c14bde3eb1acd20ab6842166a258 Mon Sep 17 00:00:00 2001 From: Pauli Date: Tue, 10 Jul 2018 18:23:59 +0300 Subject: [PATCH 3/4] Fix the class vs struct warning --- library/Core.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/Core.cpp b/library/Core.cpp index 87afdf2c9..5f800db51 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -98,7 +98,8 @@ static bool parseKeySpec(std::string keyspec, int *psym, int *pmod, std::string size_t loadScriptFiles(Core* core, color_ostream& out, const vector& prefix, const std::string& folder); namespace DFHack { -struct MainThread { +class MainThread { +public: //! MainThread::suspend keeps the main DF thread suspended from Core::Init to //! thread exit. static CoreSuspenderBase& suspend() { From 988a5a78133f6a1a7000f13d5d28e8b19ff8ae7f Mon Sep 17 00:00:00 2001 From: Pauli Date: Tue, 10 Jul 2018 18:27:06 +0300 Subject: [PATCH 4/4] Changelog entry for the fix --- docs/changelog.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index dfd788cf1..ddf959260 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -42,6 +42,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - Fixed special characters in `command-prompt` and other non-console in-game outputs on Linux/macOS (in tools using ``df2console``) - `dwarfmonitor`, `manipulator`: fixed stress cutoffs - `startdwarf`: fixed on 64-bit Linux +- `die`: Fixed windows crash in the exit handling ## API - Added to ``Units`` module: