Sideconditions in QDir-constructor?
-
Hello everyone!
I have written the following function:
(I´ve got a file with an URL in each line. I want to get the URL given in the oid-th line. For my current problem, just the first 5 lines are important.)
@
QUrl MyClass::getUrl(int oid) {
QString tmp = MyConstants::filelocation();
qdebug() << "1" << tmp;
QDir tmpDir(tmp); //here, it crashes
qdebug() << "2";
QFile* f = new QFile(tmpDir.absoluteFilePath("Objects2.dat"));
QString resultByteArray;
if (f->exists()) {
f->open(QIODevice::ReadOnly); //TODO: catch false
for (int i = 0; i < oid; i++) {
if (f->atEnd()) {
qwarning() << tmpDir.absoluteFilePath("objects.dat") << " has less than " << oid << " entries.";
return QUrl(".");
}
f->readLine();
}
resultByteArray = f->readLine();
resultByteArray.remove(QRegExp("\r"));
resultByteArray.remove(QRegExp("\n"));
f->close();
} else {
qwarning() << "Failed to open " << tmpDir.absoluteFilePath("Objects2.dat") << ".";
}
delete f;
QString result = resultByteArray;
return QUrl(result);
}
@
I call this function twice in my program. In one case, it always works and in a second, it always crashes, when I call the constructor of QDir.
Are there any sideconditions, which must be fullfilled, so that I can call the constructor successfully?
By the way: tmp (my filelocation) is the same in both situations...Thanks a lot,
Maria.
-
There are no special conditions to be fulfilled. You might be experiencing a memory corruption or something like that. These can pop up their ugly heads at seemingly totally unrelated pieces of code. Try using valgrind or something like that to check for these.
-
Afaik, QDir has no sideconditions.
Can you check, what the debugger gives you as call stack and where exactly it crashes?
also, what content the variables have exactly -
Thanks to both of you for your fast replies!
Though I don´t like your answers (just kidding :D).
As I´m working on Windows (and using Netbeans, which somehow does not really help me debugging), I´ll install a virtual machine to examine my code properly.
I´ll report, when I´ve got some new findings (or questions).