Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Crash when calling QVector<QPoint>

Crash when calling QVector<QPoint>

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 7 Posters 2.4k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cowbie10831
    wrote on last edited by
    #1

    In my project, I use QVector to store QPoints. Everything goes well when I set up the vector in a member function, but the program crashes when that vector is called in a for loop.

    The strange thing is, the function that creates the vector and the for loop is in the same class. I also found that it will also crash when the vector is called in the constructor, or in other classes via get function.

    Here's how the function and the for loop works:

    void PacmanMap::setPath(int x1, int y1, int x2, int y2)
    {
        if(x1==x2)
        {
            for(int y=y1; y<y2; ++y)
            {
                p.setX(x1);
                p.setY(y);
                if(!pathPoints.contains(p))
                {
                    pathPoints.push_front(p);
                    if(y%10==0)
                    {
                        if(p!=p1 && p!=p2 && p!=p3 && p!=p4)
                            dotPoints.push_front(p);
                    }
                }
            }
        }
        else if(y1==y2)
        {
            for(int x=x1; x<x2; ++x)
            {
                p.setX(x);
                p.setY(y1);
                if(!pathPoints.contains(p))
                {
                    pathPoints.push_front(p);
                    if(x%10==0)
                    {
                        if(p!=p1 && p!=p2 && p!=p3 && p!=p4)
                            dotPoints.push_front(p);
                    }
                }
            }
        }
    }
    

    and the for loop:

    bool PacmanMap::canMove(QPoint point)
    {
        for(int i=0; i<dotPoints.size(); ++i)//crashes at this line
        {
            if(dotPoints[i]==point)
                return true;
        }
        return false;
    }
    

    I've tried with qDebug for several times and I'm 100% sure that it's the QVector that causes the crash, but totally have no clue about how this happens. Please help me fix this, thank you!!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      I don't see anything fundamentally wrong in your code snippets.

      What does the stack trace of the crash tell you ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        Can you recreate the problem in a small test program? This will help you determine if it is even related to the QVector and not something else. Some problems are tricky and move around on you.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          arsinte_andrei
          wrote on last edited by arsinte_andrei
          #4

          have you tried to use foreach instead of for loop?

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet,

            I don't see anything fundamentally wrong in your code snippets.

            What does the stack trace of the crash tell you ?

            C Offline
            C Offline
            Cowbie10831
            wrote on last edited by
            #5

            @SGaist I found it's actually a segmentation fault...
            the number "5344" is the size of pathPoints.
            0_1559315747167_Screenshot from 2019-05-31 23-08-06.png

            But the strange thing is, when the <pathpoint> vector is called in the same way in other function, things still go very well like this:

            void PacmanMap::drawDots()
            {
                QPainter painter;
                painter.begin(&mappic);
                painter.setRenderHint(QPainter::Antialiasing);
                painter.setPen(Qt::NoPen);
                painter.setBrush(Qt::magenta);
            
                for(int i=0;i<pathPoints.size();i++)
                    painter.drawEllipse(pathPoints[i].x()-2,pathPoints[i].y()-2,4,4);
            }
            

            @fcarney sorry I don't get, what do you mean a test program?
            @arsinte_andrei I've tried it but nothing changed, thanks anyway :)

            fcarneyF Pl45m4P 2 Replies Last reply
            0
            • C Cowbie10831

              @SGaist I found it's actually a segmentation fault...
              the number "5344" is the size of pathPoints.
              0_1559315747167_Screenshot from 2019-05-31 23-08-06.png

              But the strange thing is, when the <pathpoint> vector is called in the same way in other function, things still go very well like this:

              void PacmanMap::drawDots()
              {
                  QPainter painter;
                  painter.begin(&mappic);
                  painter.setRenderHint(QPainter::Antialiasing);
                  painter.setPen(Qt::NoPen);
                  painter.setBrush(Qt::magenta);
              
                  for(int i=0;i<pathPoints.size();i++)
                      painter.drawEllipse(pathPoints[i].x()-2,pathPoints[i].y()-2,4,4);
              }
              

              @fcarney sorry I don't get, what do you mean a test program?
              @arsinte_andrei I've tried it but nothing changed, thanks anyway :)

              fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #6

              @Cowbie10831 said in Crash when calling QVector<QPoint>:

              sorry I don't get, what do you mean a test program?

              A separate program with the minimal amount of code to reproduce the problem. It is often done to isolate code and also make it so other people can test the program. If submitting a bug report it is often required.

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              0
              • C Cowbie10831

                @SGaist I found it's actually a segmentation fault...
                the number "5344" is the size of pathPoints.
                0_1559315747167_Screenshot from 2019-05-31 23-08-06.png

                But the strange thing is, when the <pathpoint> vector is called in the same way in other function, things still go very well like this:

                void PacmanMap::drawDots()
                {
                    QPainter painter;
                    painter.begin(&mappic);
                    painter.setRenderHint(QPainter::Antialiasing);
                    painter.setPen(Qt::NoPen);
                    painter.setBrush(Qt::magenta);
                
                    for(int i=0;i<pathPoints.size();i++)
                        painter.drawEllipse(pathPoints[i].x()-2,pathPoints[i].y()-2,4,4);
                }
                

                @fcarney sorry I don't get, what do you mean a test program?
                @arsinte_andrei I've tried it but nothing changed, thanks anyway :)

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @Cowbie10831

                Idk if it's just a typo but in your post the name of the vector, you are using is "dotPoints", but the vector in the screenshot is "pathPoints"?!

                How exactly the crash looks like? Do you get a debug error / app crash or can you see any window?

                I would say, you try to access a vector element out of vector range anywhere...

                Debug the ranges from all involved vectors before and after the loops. In most cases, you will find the error.
                Or as @fcarney already mentioned: Create a new project without all the game-logic stuff and debug the vectors there.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                C 1 Reply Last reply
                2
                • Pl45m4P Pl45m4

                  @Cowbie10831

                  Idk if it's just a typo but in your post the name of the vector, you are using is "dotPoints", but the vector in the screenshot is "pathPoints"?!

                  How exactly the crash looks like? Do you get a debug error / app crash or can you see any window?

                  I would say, you try to access a vector element out of vector range anywhere...

                  Debug the ranges from all involved vectors before and after the loops. In most cases, you will find the error.
                  Or as @fcarney already mentioned: Create a new project without all the game-logic stuff and debug the vectors there.

                  C Offline
                  C Offline
                  Cowbie10831
                  wrote on last edited by Cowbie10831
                  #8

                  @Pl45m4 pathpoints is the one I’m actually using, I forgot to change it back when making this post. But the results are same though, both vector in the function “bool canMove” lead to segmentation fault.

                  Pl45m4P 1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi
                    If app crash by simply calling size() on the vector, i would suspect the class instance you are in, is invalid.
                    Is there any chance pacManMap instance can have gone out of scope `?

                    1 Reply Last reply
                    3
                    • C Cowbie10831

                      @Pl45m4 pathpoints is the one I’m actually using, I forgot to change it back when making this post. But the results are same though, both vector in the function “bool canMove” lead to segmentation fault.

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #10

                      @Cowbie10831

                      You said, when you call another function which uses pathPoints' size for looping, it works?!
                      What happens before "canMove" is called?
                      Are you sure, that the for-loop causes the crash and not the if-clause inside (pathPoints . at (i) == points)?

                      EDIT:
                      According to this (https://stackoverflow.com/questions/19615371/segmentation-fault-due-to-vectors) the usage of "vector[ i ]" does not check vector bounds in terms of seg fault, while "vector . at( i )" does.
                      I guess this is, why your function "drawDots()" "works" / does not crash the app (you use pathPoints[ i ] there).
                      So there must be something wrong with your pathPoint-vector in general.


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      1
                      • C Offline
                        C Offline
                        Cowbie10831
                        wrote on last edited by
                        #11

                        @mrjj if it's actually out of scope, what is the possible way to fix it? Does declare the vector as a static vector help?

                        @Pl45m4 I'm pretty sure about that because the screenshot above is the last step before the crash. If I set the breakpoint at line 103, it would crash directly.

                        It starts from a public slot "updater()" in mainwindow.cpp, then a function "Pacman::move()" is called. The "move()" is overriding a virtual function in another abstract class. Inside move(), after some switch statement, "PacmanMap::canMove()" is called.

                        I tried both "pathPoints[i]" and "pathPoints.at(i)", and the results are same.

                        I've just tried to call "PacmanMap::drawDots()" instead of "canMove()" inside "Pacman::move()", and the program crashes, too. "drawDots()" was originally called in the constructor of class "PacmanMap" and it works. Does this help? I still have no clue after this try. Here's the screenshot of debugging:
                        0_1559400184881_Screenshot from 2019-06-01 22-40-59.png

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          You did not initialize your PacmanMap (member)pointer inside Pacman and therefore your instance is invalid. This was already mentioned by @mrjj this morning and can now be seen very good - your this pointer is a nullptr.

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          3
                          • C Offline
                            C Offline
                            Cowbie10831
                            wrote on last edited by Cowbie10831
                            #13

                            Thanks for everyone's help, it works after initializing the pointer!

                            1 Reply Last reply
                            2

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved