Hi all,
I'm running Qt 4.0.1, compiled with gcc 3.4.2 under Windows XP.
I ran into a strange problem while trying to found a way to have only
one instance of my application running at a given time. The way I'm
trying to do it is to create an empty file at the beginning and remove
it if my app is exiting normally. So, if this file exists I should
block the user saying that my application is already running or, worst
case, it crashes. When I'm launching a second application while one is
already running, it simply crashes on the first QFile call that is
using it, calling a static member or not.
Here is the code I'm using:
int main(int argc, char *argv[])
{
MediumApplication app(argc, argv);
/* Check if Medium is already running
by using a small hack. In crash cases,
this hack can lead the application to
believe that an instance is already running */
QFile lockFile("medium.lock"); //// <---- CRASH HERE WHEN FILE
ALREADY EXISTS
if( !lockFile.exists() )
{
lockFile.open(QIODevice::WriteOnly);
lockFile.close();
}
else
{
QMessageBox::critical(0, MediumApplication::applicationName(),
QObject::tr("An instance of Medium should
be already running.\n"
"Please exit the existing instance.\n\n"
"In case Medium crashed, please delete
the file medium.lock located at\n", "lock") +
MediumApplication::applicationDirPath(),
QObject::tr("Exit", "lock"));
}
MediumMainWindow *mw = new MediumMainWindow;
mw->show();
int code = app.exec();
// Remove lock
lockFile.remove();
return code;
}
Does someone know what am I doing wrong and/or if there is better way
to do that ?
Thanks by advance :)
Denys