We help IT Professionals succeed at work.

Hinder app to start twice

Ingo Förster
Last Modified: 2020-10-11
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");
        //OK, we now dissallow the app to start twice but we need the possibility to push up the existing window.

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");
        //OK, we now dissallow the app to start twice but we need the possibility to push up the existing window.

    auto teststyle = R"(
                     font-size: 13px;
                     font-weight: bold;


    QTranslator pr;

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

            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;

    return a.exec();

Open in new window

Watch Question

ste5anSenior Developer

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

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


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.

Architect - Coder - Mentor
This one is on us!
(Get your first solution completely free - no credit card required)

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: 'Eduard Ghergu' (https:#a42993791)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.