Hinder app to start twice

Ingo Förster
Ingo Förster used Ask the Experts™
on
In my QT I added a function to avoid double startup of the app. I decided to use a memory compare function
This looks like:

    QSharedMemory shared("61BB200D-3579-453e-9044-#58");
    if(!shared.create(512,QSharedMemory::ReadWrite)==true)
    {
        //OK, we now dissallow the app to start twice but we need the possibility to push up the existing window.
        exit(0);
    }

Open in new window


The problem is that this function is not working very good. If I start my app in QTCreator it looks like that his memory block exists all the time, also if I
stop the app. This course very often with Debug. Then I have to increase the last number to bring the app to start.

Do I miss something when end the app?

The whole main.cpp looks like
#include "mainwindow.h"
#include <QApplication>
#include <QTranslator>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QSharedMemory shared("61BB200D-3579-453e-9044-#58");
    if(!shared.create(512,QSharedMemory::ReadWrite)==true)
    {
        //OK, we now dissallow the app to start twice but we need the possibility to push up the existing window.
        exit(0);
    }

    auto teststyle = R"(
                   QGroupBox{
                     font-size: 13px;
                     font-weight: bold;
                     }
                   )";


    a.setStyleSheet(teststyle);
    a.setApplicationVersion(APP_VERSION);

    QTranslator pr;

    QSettings settings("IFoerster", "Diskbutler");
    int nLanguage = settings.value("language",0).toInt();

    if(nLanguage!=0){
        if(nLanguage==1031){
            pr.load( "./diskbutler_de.qm" );
        }
        a.installTranslator( &pr);
    }

    //For symbol button usage
    if (QFontDatabase::addApplicationFont(":/res/FontAwesome.otf") < 0)
        qWarning() << "FontAwesome cannot be loaded !";

    MainWindow w;
    w.show();

    return a.exec();
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
I'm not a C++ specialist, but when looking into the QSharedMemory sample, you should write an additional constant string to it. Then your other process reads it and quits if that string is found.
What OS is this running on,  from the docs:
Unix: QSharedMemory "owns" the shared memory segment. When the last thread or process that has an instance of QSharedMemory attached to a particular shared memory segment detaches from the segment by destroying its instance of QSharedMemory, the Unix kernel release the shared memory segment. But if that last thread or process crashes without running the QSharedMemory destructor, the shared memory segment survives the crash.

Is it possible that your debug testing is not allowing a clean termination of the process/threads?
You could try calling shared.detach() prior to returning.
Top Expert 2016

Commented:
i would recommend the following:

first, check the error code if create function failed. if it isn't QSharedMemory::AlreadyExists you may have some other issues.

second, don't exit with

exit(0);

Open in new window


but simply

return 0;

Open in new window


or better

return shared.error();

Open in new window



the exit function wouldn't destroy all objects depending on your OS.

anyway, if the debugger is involved, you may have other issues with destroying system objects. so, to make your system more robust, you could ignore when the shared memory already exists and check for some contents written to the shared memory. so, you can write "heart beats" by your active program, either wriiten by a timer (of your gui) or by a thread. if your program exits the life signals wouldn't be up-to-date and then your program ignore the 'already exists' and attach to the shared memory in order to write its own life beat.

Sara

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial