Results 1 to 6 of 6

Thread: QValueVector in Qt 3(.3.8) is broken

  1. #1
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Unhappy QValueVector in Qt 3(.3.8) is broken

    I have put a lot of effort to rewrite huge part of my app to work with QValueVector but all in vain. Below I present proof of this broken component. Correct me if I'm wrong and I'll be happy.

    My code:
    Qt Code:
    1. qDebug(">");
    2. playlistsList::iterator i, j;
    3. i = playlists.begin();
    4. j = i;
    5. i += where;
    6.  
    7. //qDebug(playlists.at(which).name);
    8. //qDebug((*i).name);
    9.  
    10. for (int i = 0 ; i != playlists.count() ; i++, j++)
    11. {
    12. qDebug(playlists.at(i).name + " " + (*j).name);
    13. }
    14.  
    15. qDebug("i'll insert " + playlists.at(which).name + " before " + (*i).name);
    16.  
    17. playlists.insert(i, playlists.at(which));
    18.  
    19. for (int i = 0 ; i != playlists.count() ; i++)
    20. qDebug(playlists.at(i).name);
    21.  
    22. qDebug("after redundant removed");
    23.  
    24. i = playlists.begin();
    25. j = i;
    26. i += (which + 1);
    27.  
    28. playlists.erase(i);
    29.  
    30. for (int i = 0 ; i != playlists.count() ; i++, j++)
    31. qDebug(playlists.at(i).name + " " + (*j).name);
    32.  
    33. qDebug("");
    34. qDebug("");
    To copy to clipboard, switch view to plain text mode 

    Results after first execution of above code are correct:
    Qt Code:
    1. >
    2. aa aa
    3. bb bb
    4. i'll insert bb before aa
    5. bb
    6. aa
    7. bb
    8. after redundant removed
    9. bb bb
    10. aa aa
    To copy to clipboard, switch view to plain text mode 

    Second execution gives INCORRECT result:
    Qt Code:
    1. >
    2. bb bb
    3. aa aa
    4. i'll insert aa before bb
    5. bb
    6. bb
    7. aa
    8. after redundant removed
    9. bb bb
    10. bb bb
    To copy to clipboard, switch view to plain text mode 

    Can anyone help me?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QValueVector in Qt 3(.3.8) is broken

    Could you post a minimal compilable example?

  3. #3
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QValueVector in Qt 3(.3.8) is broken

    Qt Code:
    1. /**
    2. Broken QValueVector demo (Qt 3.3.8, GCC 4.1.1)
    3. @author amdfanatyk
    4.  
    5. compilation: c++ qvalvecshit.cpp -o valvec -lqt-mt
    6. **/
    7.  
    8. #include <qstring.h>
    9. #include <qvaluevector.h>
    10.  
    11. struct elem
    12. {
    13. elem() {}
    14. elem(const QString & _s1, const QString & _s2) { s1 = _s1; s2 = _s2; }
    15.  
    16. QString s1;
    17. QString s2;
    18. };
    19.  
    20. typedef QValueVector<elem> elemsList;
    21.  
    22. void printList(const elemsList & list)
    23. {
    24. qDebug("---");
    25.  
    26. for (int i = 0 ; i != list.count() ; i++)
    27. qDebug(list[i].s1 + " " + list[i].s2);
    28.  
    29. qDebug("---");
    30. }
    31.  
    32. void insertElem(elemsList & e)
    33. {
    34. if (e.count() < 2)
    35. return;
    36.  
    37. elemsList::iterator i = e.begin();
    38.  
    39. printList(e);
    40.  
    41. QString e1s1 = e.at(1).s1;
    42. QString e0s1 = (*i).s1;
    43.  
    44. qDebug("*- i'll insert " + e1s1 + " before " + e0s1);
    45.  
    46. e.insert(i, e.at(1));
    47.  
    48. printList(e);
    49.  
    50. if ((e.at(0).s1 != e1s1) || (e.at(1).s1 != e0s1))
    51. qDebug("Ooops!");
    52. else
    53. qDebug("OK");
    54. }
    55.  
    56. int main()
    57. {
    58. elemsList elems;
    59.  
    60. elem e1("a", "aa");
    61. elem e2("b", "bb");
    62.  
    63. elems.append(e1);
    64. elems.append(e2);
    65.  
    66. insertElem(elems);
    67. insertElem(elems);
    68.  
    69. return 0;
    70. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ---
    2. a aa
    3. b bb
    4. ---
    5. *- i'll insert b before a
    6. ---
    7. b bb
    8. a aa
    9. b bb
    10. ---
    11. OK
    12. ---
    13. b bb
    14. a aa
    15. b bb
    16. ---
    17. *- i'll insert a before b
    18. ---
    19. b bb
    20. b bb
    21. a aa
    22. b bb
    23. ---
    24. Ooops!
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QValueVector in Qt 3(.3.8) is broken

    I've changed
    Qt Code:
    1. e.insert(i, e.at(1));
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. elem el = e.at(1);
    2. e.insert(i, el);
    To copy to clipboard, switch view to plain text mode 
    and it seems to work OK now.

    IMO the problem is that at() returns a reference, which is valid only while the vector remains unchanged. But insert() has to make space for the new item, so it moves existing items and the reference becomes invalid.

  5. #5
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QValueVector in Qt 3(.3.8) is broken

    Thanks. Now it works but I don't think I'll use QValueVector and QTL. For me that's a bug. insert() should insert a copy of, so first it should make a copy and then insert it. To get it work I must make one copy and then vector makes another copy and I waste a lot of memory, it sucks. When I use my own engine I only change values of pointers - there is no need to make copies and remove redundant objects. With QPtrVector I think I'll have similar problems.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QValueVector in Qt 3(.3.8) is broken

    Quote Originally Posted by fear View Post
    For me that's a bug. insert() should insert a copy of, so first it should make a copy and then insert it.
    insert() itself is OK. The problem is that the item is passed by reference.

    Quote Originally Posted by fear View Post
    When I use my own engine I only change values of pointers - there is no need to make copies and remove redundant objects.
    That's how QPtrVector works. QValueVector, just like std::vector, stores items, not pointers, so it has to copy them. But if you have your own collection that suits you better, just use it.

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.