Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

need help SigSegv fault with normal + calculation



  • hi there;
    i need help to figure out my problem
    everytime i try to debugg it there is a sigsegv fault
    here are my classes:

    #include "munition.h"
    #include "gegner.h"
    #include "mainwindow.h"
    #include "game.h"
    #include "punkte.h"
    #include "QTimer"
    #include "QDebug"
    #include "QGraphicsScene"
    extern MainWindow * mainwindow;
    Munition::Munition()
    {
    //rect zeichnen
    setRect(0,0,2,10);

    //connect
    QTimer *timer =new QTimer();
    connect(timer,SIGNAL(timeout()),this,SLOT(move()));
    timer->start(40);
    

    }

    void Munition::move()
    {

    //Punkte * punkte;
    //muni kollidiert
    QList<QGraphicsItem *> colliding_items=collidingItems();
    for(int i=0,n=colliding_items.size();i<n;i++)
    {
        if(typeid (*(colliding_items[i]))==typeid(Gegner))
        {
    
            punkten->punkten();//here i go to the other class
            //mainwindow->punkte->punkten();
            //both removen
            scene()->removeItem(colliding_items[i]);
            scene()->removeItem(this);
            //delete both
            delete colliding_items[i];
            delete this;
            return;
        }
    
    }
    
    //move muni up
    setPos(x(),y()-10);
    
    if(pos().y()<0)
    {
        scene()->removeItem(this);
        delete this;
    
    }
    

    }

    #include "punkte.h"
    #include "QFont"
    using namespace std;
    Punkte::Punkte(QGraphicsItem *parent): QGraphicsTextItem(parent)
    {
    punkte=0;
    plus=250;

    setPlainText(QString("punkte:")+QString::number(punkte));
    setDefaultTextColor(Qt::red);
    setFont(QFont("times",16));
    

    }

    void Punkte::punkten()
    {

    punkte=plus+punkte;//here the SEGSEGV Fault happends 
    setPlainText(QString("punkte:")+QString::number(punkte));
    return;
    

    }

    int Punkte::getPunkte()
    {
    return punkte;
    }


  • Moderators

    @JonnyQB said in [need help SigSegv fault with normal + calculation]

    that‘s most likely an incorrect jump into your source file, happens.

    I‘m way more concerned about the delete this; inside a Slot of a Qobject based clas!!!
    don‘t do that, rather call deleteLater()



  • @J-Hilk
    ok i will try this



  • now i tried it with global but there is the same problem


  • Lifetime Qt Champion

    Hi
    Are you sure that punkten-> is a valid pointer ?
    You do not seem to assign to it before calling it.

    QList<QGraphicsItem *> colliding_items=collidingItems();
    for(int i=0,n=colliding_items.size();i<n;i++)
    {
        if(typeid (*(colliding_items[i]))==typeid(Gegner))
        {
    
            punkten->punkten();// where do u set punkten to anything valid ?
    


  • im my header file

    class Munition : public QObject,public QGraphicsRectItem
    {
    Q_OBJECT
    public:
    Punkte * punkten;
    Munition();
    public slots:
    void move();

    };

    #endif // MUNITION_H


  • Lifetime Qt Champion

    @JonnyQB
    Hi
    That is a dangling pointer.
    It will point to random memory.
    So if you do not assign it anywhere, that is the reason you crash.

    Like
    punkten = new Punkte();



  • @mrjj
    as what schould i declare it
    punkten=new Punkte(); got the c++ requiers a type specifier...


  • Lifetime Qt Champion

    @JonnyQB
    Hi
    You declare it like
    Punkte * punkten;

    Then some place else you must do
    punkten = new Punkte(); ( not in .h)
    Often the constructor is a good place.