Results 1 to 20 of 26

Thread: wrong order of elements in QList

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2013
    Posts
    18
    Qt products
    Qt4

    Question wrong order of elements in QList

    Hi all,

    I'm trying to find out the cause of a bug in an open source project (LibreCAD). I am at my wit's end.
    The problem is (from the perspective of a user) that some arcs are drawn in the wrong direction.
    Here is an example:


    The left half of the image is the way how it should look like. On the right side you can see the bug.
    It's an image of a 0,0/200,100 rectangle which should be filled with a hatch (the hatch is called "arcs")

    What some other guys and I found out so far:
    #this bug seems to be present in release version only
    #may!!! only effect 32bit os

    My recent debugging orgy led to these results:
    The order of the start point and end point of the arc to be drawn are transposed in the QList "is2":
    Qt Code:
    1. QList<std::shared_ptr<RS_Vector> > is2;
    To copy to clipboard, switch view to plain text mode 

    As "is2" is created/filled in the sort section I think the bug have to be somewhere there.
    Here is the code snippet of the sorting:

    Qt Code:
    1. ...
    2. // sort the intersection points into is2:
    3. RS_Vector sp = startPoint;
    4. double sa = center.angleTo(sp);
    5. if(ellipse != NULL) sa=ellipse->getEllipseAngle(sp);
    6. QList<std::shared_ptr<RS_Vector> > is2;
    7. bool done;
    8. double minDist;
    9. double dist = 0.0;
    10. std::shared_ptr<RS_Vector> av;
    11. std::shared_ptr<RS_Vector> v;
    12. RS_Vector last = RS_Vector(false);
    13. do {
    14. done = true;
    15. minDist = RS_MAXDOUBLE;
    16. av.reset();
    17. for (int i = 0; i < is.size(); ++i) {
    18. v = is.at(i);
    19. double a;
    20. switch(e->rtti()){
    21. case RS2::EntityLine:
    22. dist = sp.distanceTo(*v);
    23. break;
    24. case RS2::EntityArc:
    25. case RS2::EntityCircle:
    26. a = center.angleTo(*v);
    27. dist = reversed?
    28. fmod(sa - a + 2.*M_PI,2.*M_PI):
    29. fmod(a - sa + 2.*M_PI,2.*M_PI);
    30. break;
    31. case RS2::EntityEllipse:
    32. a = ellipse->getEllipseAngle(*v);
    33. dist = reversed?
    34. fmod(sa - a + 2.*M_PI,2.*M_PI):
    35. fmod(a - sa + 2.*M_PI,2.*M_PI);
    36. break;
    37. default:
    38. break;
    39.  
    40. }
    41.  
    42. if (dist<minDist) {
    43. minDist = dist;
    44. done = false;
    45. av = v;
    46. }
    47. }
    48.  
    49. // copy to sorted list, removing double points
    50. if (!done && av.get()!=NULL) {
    51. if (last.valid==false || last.distanceTo(*av)>RS_TOLERANCE) {
    52. is2.append(std::shared_ptr<RS_Vector>(new RS_Vector(*av)));
    53. last = *av;
    54. }
    55. #if QT_VERSION < 0x040400
    56. emu_qt44_removeOne(is, av);
    57. #else
    58. is.removeOne(av);
    59. #endif
    60.  
    61. av.reset();
    62. }
    63. } while(!done);
    64. ...
    To copy to clipboard, switch view to plain text mode 
    The sort process should sort the points/elements in the list "is" and add them in the correct order* to "is2".
    *the order should be: start point, the point next to the start point, ..., the end point

    The funny thing (at least for me) is, when I add qDebug("angle centerToNextIntersection: %f",a);
    after these lines :
    Qt Code:
    1. case RS2::EntityArc:
    2. case RS2::EntityCircle:
    3. a = center.angleTo(*v);
    4. dist = reversed?
    5. fmod(sa - a + 2.*M_PI,2.*M_PI):
    6. fmod(a - sa + 2.*M_PI,2.*M_PI);
    7. ->here->here->
    To copy to clipboard, switch view to plain text mode 
    the weird behaviour seems to be gone!

    If you would like to have a look at the discussion of this bug in the LibreCAD forum (there are more pictures of the bug):
    http://forum.librecad.org/bugs-probl...tp5708894.html

    If you would like to have a look at the whole file where the bug is present:
    https://github.com/LibreCAD/LibreCAD...e/rs_hatch.cpp
    (the sort process begins at about the line 335)

    Are the elements of QList added/removed in the wrong way?
    What could also lead to a behaviour where the order of QList may become upside down/incorrect?

    ANY help is appreciated!
    (also hints about things I could try)

    using:
    ubuntu 12.04 32bit
    Compiler: GNU GCC 4.6.3
    Qt Version: 4.8.1

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: wrong order of elements in QList

    QList is not sorted list. Conclusion is that your sorting function is not working properly.

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: wrong order of elements in QList

    ANY help is appreciated!
    (also hints about things I could try)
    Try to insert elements to "is" in correct order to avoid the sorting function.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: wrong order of elements in QList

    It may be easier to make a comparison function two items and apply standard qsort function ?

  5. #5
    Join Date
    Oct 2013
    Posts
    18
    Qt products
    Qt4

    Default Re: wrong order of elements in QList

    Thanks for the replys guys. As I would like to understand the bug first I will try the workarounds after that.
    After you pointed me to the sorting, I did some more research.
    Can someone explain the strange stepping through the code in QtCreator?
    (the order of the steps when I use "Step Over" are placed at the end of the code lines)
    Qt Code:
    1. case RS2::EntityCircle:
    2. a = center.angleTo(*v); 1.
    3. dist = reversed?
    4. fmod(sa - a + 2.*M_PI,2.*M_PI): 3.
    5. fmod(a - sa + 2.*M_PI,2.*M_PI); 2. 4. 6.
    6. // qDebug("angle centerTostartPoint: %f",sa);
    7. // qDebug("angle centerToNextIntersection: %f",a);
    8. // qDebug("distance: %f",dist);
    9. qDebug("counted the useres: %d",v.use_count()); 5. 7.
    10. break;
    To copy to clipboard, switch view to plain text mode 

    I would have expected the following order of the stepping:
    line2
    line5 (until now that's the same what QtCreator does; reversed is false)
    line9

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: wrong order of elements in QList

    What is so strange in the order the debugger steps through the code? That's the order the code executes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2013
    Posts
    18
    Qt products
    Qt4

    Default Re: wrong order of elements in QList

    What I don't understand is, why QtCreator steps to line 4 in the 3. step. Cause reversed is false, shouldn't it just leave out line 4?
    Why does QtCreator execute line 5 again in step 4? Wasn't that already done in step 2. ?
    Why does QtCreator jump back to line 5 in step 6. for the third time?

    What I was thinking it would do:
    1. assign a ->line 2
    2. check reversed -> line 3
    3. reversed is false -> line 5
    4. assign dist -> line 3
    5. qDebug

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: wrong order of elements in QList

    Creator does not step into anything. The debugger does based on the order of which instructions are executed. Apparently the compiler builds the code in such a way, that the execution takes place in such particular order. As for "line 4" in my opinion this is a mark that the "?" instruction is executed and as it is the last symbol in line 3, the compiler (becasue of whatever reason) marks it as the next line. I really wouldn't worry about this, if you have doubts take a look at the instructions on assembly level or disable optimizations and then run the code through the debugger again. All this however does not bring you any closer to solving your original problem.

    What I was thinking it would do:
    1. assign a ->line 2
    2. check reversed -> line 3
    3. reversed is false -> line 5
    4. assign dist -> line 3
    5. qDebug
    I would think it first calculates 2.*M_PI as this is used in both cases so it can be precalculated, then it'd check the condition, calculate sa-a or a-sa based on the condition, then add the result to 2*M_PI, then execute fmod on the result, then assign it to the dist variable and finally print out the debug statement. Of course, if v.use_count() is independent of the result of "dist", the compiler might put the debug statement between any two instructions in the whole section.
    Last edited by wysota; 20th October 2013 at 08:45.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Oct 2013
    Posts
    18
    Qt products
    Qt4

    Default Re: wrong order of elements in QList

    All this however does not bring you any closer to solving your original problem.
    Ok.
    ...the compiler might put the debug statement between any two instructions in the whole section
    Didn't know that.

    So I had some more debugging fun.
    The problem is, that calculation of "a" is a little off compared to sa.
    I don't know why this happens cause both angles are calculated using the SAME function and the SAME
    point.

    Qt Code:
    1. double sa = center.angleTo(sp);
    2. ...
    3. double a;
    4. ...
    5. a = center.angleTo(*v);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. double angleTo(const RS_Vector& v) const;
    To copy to clipboard, switch view to plain text mode 

    here is sp and v from the watch list (they are the same point/coordinates)

    sp RS_Vector
    valid true bool
    x 170.15564366033121 double
    y 20.000000048775643 double
    z 0 double


    v std::shared_ptr<RS_Vector>
    __shared_ptr<RS_Vector, (__gnu_cxx::_Lock_policy)2> std::__shared_ptr<RS_Vector, (__gnu_cxx::_Lock_policy)2>
    _M_ptr @0x8ac7ff0 RS_Vector
    valid true bool
    x 170.15564366033121 double
    y 20.000000048775643 double
    z 0 double
    _M_refcount std::__shared_count<(__gnu_cxx::_Lock_policy)2>



    here is the result of a - sa (as you can see it's e-16) (should be 0 as it is the same point in this case)

    result of a - sa: -4.4408920985006261617e-16

    As I already told, adding qDebug("angle centerToNextIntersection: %f",a) will somehow lead to
    the result 0 of a - sa. Does anybody have an idea what qDebug does to "a"?
    I don't get how qDebug does influence the calculation of "a" and why the reseults of calculating
    the same angle of the same point using the same function, differ from each other (without qDebug())?

Similar Threads

  1. Errors inserting elements in a QList
    By sogico in forum Newbie
    Replies: 4
    Last Post: 16th August 2012, 23:22
  2. Replies: 6
    Last Post: 29th November 2009, 00:43
  3. deleting all elements in a qlist
    By reshma in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2009, 20:27
  4. Remove first n elements from QList
    By pakulo in forum Qt Programming
    Replies: 8
    Last Post: 4th June 2007, 07:27
  5. about QHash elements order
    By bruce1007 in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2006, 07:17

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.