6

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 :(

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
  • Works for me on Qt 5.5.0. – MrEricSir Dec 23 '15 at 23:58
  • Yup, used to work for me on Qt 5.5.0 too,,, So, I think maybe I did something wrong during development process, while playing, fiddling with it...? – סטנלי גרונן Dec 24 '15 at 00:07
  • If you need this on Windows only, use a name mutex (see [CreateMutex](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682411.aspx)) instead of Qt's abominations. See [Preventing multiple instances of my application](http://stackoverflow.com/q/8799646/1889329) for possible implementations. – IInspectable Dec 24 '15 at 01:57
  • You can use QtSingleApplication http://stackoverflow.com/questions/5006547/qt-best-practice-for-a-single-instance-app-protection – demonplus Dec 24 '15 at 08:51
  • @IInspectable: That's a Microsoft stuff, I don't think it would work under Qt (minGW compiler)... would it? – סטנלי גרונן Dec 24 '15 at 09:26
  • @demonplus : Well, it seems QtSingleApplication is not part of Qt's main/basic distribution. I suppose the issue can be solve without adding extra packages... Nowadays people tend to install as many extra packs as possible: QtExtra, Boost, etc-etc... I prefer to keep things minimal and simple (years of experience taught me so). – סטנלי גרונן Dec 24 '15 at 09:31
  • 1
    Qt (for Windows) is implemented using *"Microsoft stuff"*. Anything in the Windows API is available to MinGW. No extra packages required. – IInspectable Dec 24 '15 at 10:05

1 Answers1

1

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.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68