Commit 46eac62f authored by Leander Schulten's avatar Leander Schulten

Unix: register signal handlers to release the shared memory when the...

Unix: register signal handlers to release the shared memory when the application crashes, otherwise you can not start the application another time
parent 101d4d76
Pipeline #193415 passed with stage
in 5 minutes and 18 seconds
......@@ -49,6 +49,7 @@
#include <QTimer>
#include <chrono>
#include <cmath>
#include <csignal>
#include <id.h>
#include <limits>
......@@ -61,6 +62,16 @@
#include <QNetworkReply>
#endif
#ifdef Q_OS_UNIX
// on Unix, shared memory is not released when the program crashes, so we have to release the memory ourselves
QSharedMemory *sharedMemory = nullptr;
extern "C" void signal_handler(int /*sig*/) {
if (sharedMemory) {
sharedMemory->detach();
}
}
#endif
int main(int argc, char *argv[]) {
QSharedMemory mem(QStringLiteral("Lichteuerung Leander Schulten"));
{ // check if the app is alreandy running or should be restarted
......@@ -90,8 +101,20 @@ int main(int argc, char *argv[]) {
}
// signal that we are running
*static_cast<bool *>(mem.data()) = true;
#ifdef Q_OS_UNIX
sharedMemory = &mem;
#endif
}
error::initErrorHandler();
#ifdef Q_OS_UNIX
// register signal handler to catch a abnormal program termination to release the shared memory
std::signal(SIGTERM, signal_handler);
std::signal(SIGSEGV, signal_handler);
std::signal(SIGINT, signal_handler);
std::signal(SIGILL, signal_handler);
std::signal(SIGABRT, signal_handler);
std::signal(SIGFPE, signal_handler);
#endif
#ifdef DrMinGW
ExcHndlInit();
auto path = QStandardPaths::writableLocation(QStandardPaths::QStandardPaths::AppDataLocation);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment