PDA

View Full Version : QList problem



lvi
25th August 2008, 16:41
I have a really "funny" (not really) problem involving QList.

I have a class File that, as a member, has a QList<Part>:



class File {
public:
QList<Part> parts;
}


A file object is passed to a worker thread for processing:



class Decoder : public QThread {
public:
Decoder(File f) { file = f; }
void run();
private:
File file;
}


In the run() function, I construct the file from the parts that are associated with that file:



void Decoder::run() {
QList<Part>::iterator partIter;
for (partIter = file.parts.begin(); partIter != file.parts.end(); partIter++) {
// do stuff with *partIter
}
}

(simplified representation)

This works perfectly fine if parts holds more than 1 part. If the number of parts is 1, the loop is done twice and of course, I get a segfault in the second iteration.

For completeness I should mention that file has been passed around by signal/slot connections, e.g. someSlot(const File f).

I have no clue why this could be happening. Any ideas?

tommydent
25th August 2008, 17:26
Hi,

what exactly is:


// do stuff with *partIter

do you invalidate the iterator somehow?

lvi
25th August 2008, 17:33
The only line involving *partIter is this:



QFile partFile((*partIter).filename);
// read data from partFile and use this


I don't see how this could possibly invalidate partIter. Also, that would not explain why it works with >1 parts in the QList...

lvi
25th August 2008, 18:22
Changing


QList<Part>::iterator partIter;

to


QList<Part>::const_iterator partIter;

seems to solve the problem.
I overlooked this:


emit(fileStatus(file, status));

Even though I thought declaring the signal as fileStatus(const File, int) would prevent trouble, I highly suspect this line was the troublemaker...

I'm still not sure why declaring the const_iterator solved the problem (or did I just hide the problem?).