troubleshooting Question

Hinder app to start twice

Avatar of Ingo Förster
Ingo Förster asked on
C++* Qt creator* qt 5
5 Comments1 Solution81 ViewsLast Modified:
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);
    }

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();
}
ASKER CERTIFIED SOLUTION
Eduard Ghergu
Architect - Coder - Mentor

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros