Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDir::drives() causes crash in windows
QtWS25 Last Chance

QDir::drives() causes crash in windows

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 904 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mfaghani
    wrote on last edited by mfaghani
    #1

    I'm using QDir::drives() to make a list of drives in win7. I put the code below in constructor of mainwindow.cpp

    QDir dir3;
    foreach(QFileInfo item, dir3.drives() )
    {
        qDebug() << item.absoluteFilePath();
    }
    

    I can see my drives' list in Application Output Window, but the program crashes after that. And it works well when I remove the code above.
    The error I get in Application Output Window is "The program has unexpectedly finished."

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What version of Qt are you using ?
      Can you provide a stack trace ?
      By the way, QDir::drives is a static method.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        What version of Qt are you using ?
        Can you provide a stack trace ?
        By the way, QDir::drives is a static method.

        M Offline
        M Offline
        mfaghani
        wrote on last edited by mfaghani
        #3

        @SGaist
        Thanks. I'm using Qt 5.4.0 and Qt Creator 3.3.0.
        I also tryed as static method.

        QFileInfoList fil = QDir::drives();
        QMessageBox *msg;
        qDebug() << fil.count();
        msg->setText(QString::number(fil.count()));
        msg->exec();
        

        But I've got the same result.
        qDebug() shows the number of drives correctly. But program crashes after that. I found that it happens when I use the instance of QFileInfoList (fil).
        And sorry for providing a stack trace. I don't how to do that.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Qt 5.4.0 is outdated, you should update to a more recent version. Current is Qt 5.11.2 with Qt 5.12 around the corner.

          If you need an LTS version current is Qt 5.9.7.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            For the stack trace, start your application using the debugger.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              Might be copy & paste error
              QMessageBox *msg; << dangling pointer
              msg->setText(QString::number(fil.count())); << crash here

              M 1 Reply Last reply
              2
              • mrjjM mrjj

                Hi
                Might be copy & paste error
                QMessageBox *msg; << dangling pointer
                msg->setText(QString::number(fil.count())); << crash here

                M Offline
                M Offline
                mfaghani
                wrote on last edited by mfaghani
                #7

                @mrjj said in QDir::drives() causes crash in windows:

                msg->setText(QString::number(fil.count())); << crash here

                It happens at the line which you mentioned, when I use fil.count().
                Sorry I don't understand what you mean of copy and paste error.
                In the first code I posted, I didn't use messagebox, I just used foreach loop for QFileInfo.absoluteFilePath() and I don't know if there was a copy and paste error.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  What @mrjj meant is that you are using an uninitialised pointer hence potentially the crash you see.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  M 1 Reply Last reply
                  3
                  • SGaistS SGaist

                    What @mrjj meant is that you are using an uninitialised pointer hence potentially the crash you see.

                    M Offline
                    M Offline
                    mfaghani
                    wrote on last edited by
                    #9

                    @SGaist
                    Thank you very much,
                    I modified it to QMessageBox *msg = new QMessageBox(); and every things went well. Anyway, it looks like a stupid mistake and I need to be more careful.
                    Thanks again

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved