Crash when calling QVector<QPoint>



  • 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!!


  • Lifetime Qt Champion

    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 ?



  • 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.



  • have you tried to use foreach instead of for loop?



  • @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 :)



  • @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.



  • @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.



  • @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.


  • Lifetime Qt Champion

    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 `?



  • @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.



  • @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


  • Qt Champions 2018

    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.



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


Log in to reply