qt-interest@trolltech.com
[Top] [All Lists]

Re: [Qt4] Crashing when trying to access a file

Subject: Re: [Qt4] Crashing when trying to access a file
From: denys
Date: Sun, 21 Aug 2005 13:26:56 +0200
denys a écrit :

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

Sorry, I just saw that I didn't indicate the line where it crashes. It's done now.

Denys

        

        
                
___________________________________________________________________________ Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger Téléchargez cette version sur http://fr.messenger.yahoo.com

<Prev in Thread] Current Thread [Next in Thread>