Program crashes when trying to delete Canon ( canon ball )



  • I press space and i shoot but when canon ball reaches "water" it should delete itself but instead it crashes which normally means i'm trying to access memory which no longer exists.... but i don't know what to do...

    and these are the error i get:
    0_1553625982004_e123a3fe-0f3b-4341-898a-7df9c74741f9-image.png

    QGraphicsItem::x

    0_1553626036629_04676815-fdd2-4b68-8a84-d5d163d4419f-image.png

    function inside Canon Class moveCanonBall()

    0_1553626076753_df783826-db6a-497d-836a-63b04d98882f-image.png

    and this

    0_1553626110929_5fa6d18b-2fda-4e59-8174-fbad11afb91e-image.png

    This is Canon's header file:
    0_1553626146114_519b4782-56d0-4ca8-a6ab-58a5904b6499-image.png

    and this is Canon's source file:

    #include "canon.h"
    #include "game.h"
    #include "math.h"
    #include "soundeffects.h"
    #include <QGraphicsPixmapItem>
    #include <QPixmap>
    #include <QObject>
    #include <QTimer>
    #include <QDebug>
    #include <QMediaPlayer>
    
    #include "soundeffects.h"
    
    extern Game *game;
    
    bool Canon::CanonBallGone=false;
    
    Canon::Canon(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
    
        ballHitEnd = false;
    
        setPixmap(QPixmap(":/Images/Canon_1.png")); // 30x25
        SoundEffects::canonBallShot(1);
        game->scene->addItem(this);
    
        // bullet cords
        moveByY = 1.0;
        moveDown = false;
        splashLasting = 0;
    
        // shrani direction katero player trenutno is facing // saves the direction which the player is currently facing
        // (left==3, right ==1)
        direction = game->player->getLeftOrRightDirection();
        if(direction == 1){
            xPl = game->player->x()+83;
            yPl = game->player->y()+42;
            newX = game->player->x()+83;
        }else if(direction == 3){
            xPl = game->player->x()-18;
            yPl = game->player->y()+42;
            newX = game->player->x()-18;
        }
    
        time = new QTimer();
        connect(time,SIGNAL(timeout()),this,SLOT(moveCanonBall()));
        time->start(5);
    }
    
    Canon::~Canon(){
        delete time;
    }
    
    bool Canon::getCanonBallGone(){
        return CanonBallGone;
    }
    
    bool Canon::setCanonBallGoneToFalse(){
        CanonBallGone = false;
    }
    
    bool Canon::setCanonBallGoneToTrue(){
        CanonBallGone = true;
    }
    
    
    void Canon::moveCanonBall(){
    
        if(direction==1 && ballHitEnd==false){
            // s pomocjo balisticne enacbe zracunamo y  // with a help of this function we calculate y  ( parabolic movement of the canon ball )
            help = (0.5*(x()-xPl)*tan(0.6)-(0.5 *pow((x()-xPl),2))
                    / (2*pow(20,2)*pow(cos(0.6),2))+yPl)-yPl;
            moveByY = yPl - help;
            setY(moveByY);
            setPos(x(),y());
    
            // povecujemo x za 1.5  // incrementing x by 1.5
            setX(x()+1.5);
    
            // ce je canon ball v vodi spremenimo sliko v splash sliko  // if canon ball hits its final destination we change picture to a splash 
            if(y() >= yPl+32){
    
                ballHitEnd = true;
                setPixmap(QPixmap(":/Images/Splash_1.png"));
            }
    
        }
    
    
        else if(direction==3 && ballHitEnd == false){
    
            help = (0.5*(newX-xPl)*tan(0.6)-(0.5 *pow((newX-xPl),2))
                    / (2*pow(20,2)*pow(cos(0.6),2))+yPl)-yPl;
            moveByY = yPl - help;
            setY(moveByY);
            setPos(x(),y());
    
            // povecujemo x za 1.5 // incrementing x by 1.5
            newX = newX + 1.5;
            setX(x()-1.5);
    
            // ce je canon ball v vodi spremenimo sliko v splash sliko // if canon ball hits its final destination we change picture to a splash
            if(y() >= yPl+32){
    
                ballHitEnd = true;
                setPixmap(QPixmap(":/Images/Splash_2.png"));
            }
        }
    
    
    
        // slika ostane nekaj sekund nato se canon sporsti s pomnilnika // picture stays as a splash for couple of mikroseconds then we 
          delete the canon ball from memory
        if(ballHitEnd == true){
    
            splashLasting++;
    
            if(splashLasting == 15){
                game->scene->removeItem(this);
                CanonBallGone=true;
                delete this;
           }
        }
        // ce je canon ball sou izven scene ga sporstimo iz pomnilnika // if canon ball is out of the scene we remove it   //from the scene 
         and delete it
        if(x()<(0-30) || x()>game->getW()+30){
            qDebug() << "deleted canonball";
            game->scene->removeItem(this);
            delete time;
            CanonBallGone=true;
            delete this;
        }
    }
    

    Please if anyone has any ideas on how to fix this please let me know !!!


  • Lifetime Qt Champion

    Hi,

    First thing before digging further: why does your cannon class know that much about its container and the rest ?
    You should rather have a "game engine" that handle that kind of stuff. Why should a canon be responsible for its own deletion ?

    You should also check this post about the use of delete this;



  • @SGaist

    I'm still a begginer at stuff like this and atm im making a simple game, i already figured out the problem to this so im going to mark this as solved but the thing is, i want to make sure that when my canon reaches its destination that it deletes itself and the best way to do that was to do it inside the class.... I believe.... and about the things i included, some of them probably aren't needed...

    And i'll check the post u sent me, thank you !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.