Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt5: Preventing another instance of the application doesn't work anymore...!

I am using Qt5 on a Windows7 platform.
My application is some kind of TCP server listening on port 8002, so I only want one instance of it.
In order to prevent multiple instances of my application, I use(d) the code below (found here on StackOverflow):

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

    sharedMemory.setKey("TcpServer-Key");
    if(sharedMemory.create(1) == false)
    {
       QMessageBox::warning(NULL, "Warning!", "Another instance already running!");
       a.exit(); // exit already a process running
       return 0;
    }
...

Well, the code above used to work just fine, until I upgraded my Qt to 5.5.1.
Now, with Qt 5.5.1, I don't see the warning message-box anymore!... When I try to start another instance, the running app disappears/stops and a new app is started!!!

Please help, what should I do? But don't tell me to switch back to Qt 5.4.x :(

Remark: I forgot to mention that I set & used msvc2012 compiler during tests (not minGW, as I wasn't able to build log4cxx for it).

UPDATE: Could it be an issue related to the antivirus installed on that PC (at the office, i.e. McAfee)?... Now I'm at home (AVG antivirus and MinGW compiler and log4cxx removed) and I am unable to reproduce the above described issue :(

like image 234
סטנלי גרונן Avatar asked Nov 09 '22 23:11

סטנלי גרונן


1 Answers

I finally discovered where the problem was... and it's not the antivirus to be blamed :)
When I upgraded the Qt (Creator v3.6.0) to the newest version (5.5.1), there is a setting in Tools->Options->Build&Run named [Stop app before building]... that was set to Current project or something. Hence, the Qt Creator was killing the old instance before launching a new one(!).
Setting this option to None solved the issue.
So, it seems the code was just fine, antivirus was fine, yet launching the app from within Qt Creator was somehow restricted to only one instance :)

I decided to share this, maybe it will be helpful for other folks as well.

Remark : I checked again and now I can confirm: That setting didn't exist before, at least not in Qt Creator v3.3.2.

like image 166
סטנלי גרונן Avatar answered Nov 14 '22 23:11

סטנלי גרונן