From a8f87f37f6c71751c17bf4c0d0fe214f475f9a05 Mon Sep 17 00:00:00 2001 From: Henry Cheng <39224097+jazzysoggy@users.noreply.github.com> Date: Thu, 24 Apr 2025 20:04:27 -0400 Subject: [PATCH] Proper handling of direct kill --- linux/main.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/linux/main.cpp b/linux/main.cpp index 787a133..280d5d3 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -954,9 +954,10 @@ private: int main(int argc, char *argv[]) { QApplication app(argc, argv); - // Check if app is already open QSharedMemory sharedMemory; sharedMemory.setKey("TcpServer-Key"); + + // Check if app is already open if(sharedMemory.create(1) == false) { LOG_INFO("Another instance already running! Opening App Window Instead"); @@ -968,15 +969,15 @@ int main(int argc, char *argv[]) { socket.flush(); socket.waitForBytesWritten(500); socket.disconnectFromServer(); + app.exit(); // exit; process already running + return 0; } else { - // Failed connection, log and abort - LOG_ERROR("Failed to connect to the original app instance"); + // Failed connection, log and open the app (assume it's not running) + LOG_ERROR("Failed to connect to the original app instance. Assuming it is not running."); LOG_DEBUG("Socket error: " << socket.errorString()); } - app.exit(); // exit; process already running - return 0; } app.setQuitOnLastWindowClosed(false); @@ -1043,6 +1044,11 @@ int main(int argc, char *argv[]) { LOG_DEBUG("Server error: " << server.errorString()); }); }); + + QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]() { + LOG_DEBUG("Application is about to quit. Cleaning up..."); + sharedMemory.detach(); + }); return app.exec(); }