Results 1 to 4 of 4

Thread: QList problem

  1. #1
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QList problem

    I have a really "funny" (not really) problem involving QList.

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

    Qt Code:
    1. class File {
    2. public:
    3. QList<Part> parts;
    4. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. class Decoder : public QThread {
    2. public:
    3. Decoder(File f) { file = f; }
    4. void run();
    5. private:
    6. File file;
    7. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void Decoder::run() {
    2. QList<Part>::iterator partIter;
    3. for (partIter = file.parts.begin(); partIter != file.parts.end(); partIter++) {
    4. // do stuff with *partIter
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    (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?
    Last edited by lvi; 25th August 2008 at 17:06. Reason: Corrected code

  2. #2

    Default Re: QList problem

    Hi,

    what exactly is:

    Qt Code:
    1. // do stuff with *partIter
    To copy to clipboard, switch view to plain text mode 

    do you invalidate the iterator somehow?

  3. #3
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList problem

    The only line involving *partIter is this:

    Qt Code:
    1. QFile partFile((*partIter).filename);
    2. // read data from partFile and use this
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList problem

    Changing
    Qt Code:
    1. QList<Part>::iterator partIter;
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. QList<Part>::const_iterator partIter;
    To copy to clipboard, switch view to plain text mode 
    seems to solve the problem.
    I overlooked this:
    Qt Code:
    1. emit(fileStatus(file, status));
    To copy to clipboard, switch view to plain text mode 
    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?).

Similar Threads

  1. QList, copy problems
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 5th February 2010, 00:06
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. problem with QList
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2008, 13:08
  4. QList index out of range problem
    By MarkoSan in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2008, 08:40
  5. QList problem
    By acix in forum General Programming
    Replies: 6
    Last Post: 29th April 2006, 13:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.