QDir::drives() causes crash in windows
-
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." -
Hi,
What version of Qt are you using ?
Can you provide a stack trace ?
By the way, QDir::drives is a static method. -
Hi,
What version of Qt are you using ?
Can you provide a stack trace ?
By the way, QDir::drives is a static method.@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. -
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.
-
For the stack trace, start your application using the debugger.
-
Hi
Might be copy & paste error
QMessageBox *msg; << dangling pointer
msg->setText(QString::number(fil.count())); << crash here -
Hi
Might be copy & paste error
QMessageBox *msg; << dangling pointer
msg->setText(QString::number(fil.count())); << crash here@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. -
What @mrjj meant is that you are using an uninitialised pointer hence potentially the crash you see.