Skip to content

Commit

Permalink
Cleanup shared memory on SIGTERM, SIGHUP, and SIGINT, fixes #53
Browse files Browse the repository at this point in the history
  • Loading branch information
codeThatThinks committed Apr 26, 2015
1 parent 2e414ef commit dad6b30
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <QIcon>
#include <QDir>
#include <signal.h>
#include "widget.h"
#include "hotkey.h"
#include "returnbyscript.h"
Expand All @@ -17,15 +18,27 @@
#include "config_parse.h"
#include "config.h"

SingleApplication *aptr;

using namespace std;

void quit(int sig)
{
printf("Received signal %d, quitting\n", sig);
(*aptr).quit();
}

int main(int argc, char *argv[])
{
qRegisterMetaType<QItemSelection>("QItemSelection");
SingleApplication a(argc, argv, "WEI.QIANG");
aptr = &a;
QDir *config = new QDir;
if (!config->exists(QString::fromStdString(CFPATH))) config->mkdir(QString::fromStdString(CFPATH));
delete config;
signal(SIGTERM, quit);
signal(SIGHUP, quit);
signal(SIGINT, quit);
if (a.isRunning())
{
QMessageBox* x = new QMessageBox();
Expand Down
8 changes: 8 additions & 0 deletions src/singleapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ SingleApplication::SingleApplication(int &argc, char *argv[], const QString uniq
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(checkForMessage()));
timer->start(1000);

connect(this, SIGNAL(aboutToQuit()), this, SLOT(cleanup()));
}
}

Expand All @@ -53,6 +55,12 @@ void SingleApplication::checkForMessage()
sharedMemory.unlock();
}

void SingleApplication::cleanup()
{
// clean up shared memory before quitting
sharedMemory.detach();
}

// public functions.

bool SingleApplication::isRunning()
Expand Down
1 change: 1 addition & 0 deletions src/singleapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SingleApplication : public QApplication

public slots:
void checkForMessage();
void cleanup();

signals:
void messageAvailable(QString message);
Expand Down

0 comments on commit dad6b30

Please sign in to comment.