From fe66d52a38bbc65703482db4fcfca7300d6953d9 Mon Sep 17 00:00:00 2001 From: Anton Samokhvalov Date: Wed, 29 Dec 2021 15:23:37 +0300 Subject: [PATCH] no memory leak --- src/btop.cpp | 10 +++++----- src/btop_input.cpp | 7 +++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/btop.cpp b/src/btop.cpp index ac49a7e..48884ba 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -237,15 +237,15 @@ void clean_quit(int sig) { std::cerr << Global::fg_red << "ERROR: " << Global::fg_white << Global::exit_error_msg << Fx::reset << endl; } Logger::info("Quitting! Runtime: " + sec_to_dhms(time_s() - Global::start_time)); - - const auto excode = (sig != -1 ? sig : 0); - + close(0); //? Assume error if still not cleaned up and call quick_exit to avoid a segfault from Tools::atomic_lock destructor #ifndef __APPLE__ - quick_exit(excode); + if (Tools::active_locks > 0) { + quick_exit((sig != -1 ? sig : 0)); + } #endif - exit(excode); + if (sig != -1) exit(sig); } //* Handler for SIGTSTP; stops threads, restores terminal and sends SIGSTOP diff --git a/src/btop_input.cpp b/src/btop_input.cpp index 85ba644..7a8d910 100644 --- a/src/btop_input.cpp +++ b/src/btop_input.cpp @@ -82,10 +82,14 @@ namespace Input { struct InputThr { InputThr() : thr(run, this) { + thr.detach(); } static void run(InputThr* that) { - that->runImpl(); + try { + that->runImpl(); + } catch (...) {} + delete that; } void runImpl() { @@ -120,7 +124,6 @@ namespace Input { } static InputThr& instance() { - // intentional memory leak, to simplify shutdown process static InputThr* input = new InputThr(); return *input;