D
D
Dimchik2021-04-23 06:22:00
Qt
Dimchik, 2021-04-23 06:22:00

After closing the compiled program, the error Debug Assertion Failed?

Here is the window : 60823c3bd3381340936272.png

Here is the error under Debug
60823ce854a42658693267.png
View.cpp :

#include "view.h"
#include "player.h"

#include <QObject>
#include <QTimer>
#include <QtGlobal>

View::View()
{

    setWindowTitle("Space Rangers");

    setRenderHint(QPainter::Antialiasing);

    setFrameStyle(0);
    setSceneRect(0, 0, 700,700);
    setFixedSize(700, 700);


    mainScene.setItemIndexMethod(QGraphicsScene::NoIndex);
    setScene(&mainScene);

    connect(&pl, &Player::IS_IT_Attack, this, &View::PlayerAttack);

    startGame = new QTimer(this);
    connect(startGame, &QTimer::timeout, &pl, &Player::actions);
    startGame->start(1000 / 50);
    view_elements();

    create_type1_enemy = new QTimer(this);
    connect(create_type1_enemy, &QTimer::timeout, this, &View::addType1Enemy);
    create_type1_enemy->start(8000);

    reload_virusmove = new QTimer(this);
    connect(reload_virusmove, &QTimer::timeout, this, &View::virusMov);

    T1_Enemy_Attack = new QTimer(this);
    connect(T1_Enemy_Attack, &QTimer::timeout, this, &View::Type1_EnemyAttack);

    T1_Enemy_bulletmove = new QTimer(this);
    connect(T1_Enemy_bulletmove, &QTimer::timeout, this, &View::T1_E_BulletMov);

    player_bulletmove = new QTimer(this);
    connect(player_bulletmove, &QTimer::timeout, this, &View::p_bulletMov);

    create_reload_Virus = new QTimer(this);
    connect(create_reload_Virus, &QTimer::timeout, this, &View::addVirus);
    create_reload_Virus->start(30000);

}

void View::view_elements(){

    pl.setPos(205, 400);
    mainScene.addItem(&pl);
}

void View::addVirus(){

    reload_Virus *virus = new reload_Virus();
    r_vir = new reload_Virus();
    r_vir = virus;
    virus->x = rand() % 630;
    mainScene.addItem(virus);
    virus->setPos(r_vir->x, r_vir->y);
    Viruses.append(virus);
    viruspodMoving(virus);

}

void View::delete_reload_Virus(QGraphicsItem *virus){

    foreach(reload_Virus *v, Viruses){
        if(v == virus){
            mainScene.removeItem(v);
            Viruses.removeOne(v);
            delete virus;
            r_vir->y = -70;
            r_vir->x = 0;
        }
    }
}

void View::viruspodMoving(QGraphicsItem *virus){

    if(r_vir->y < 700){
        reload_virusmove->start(40);
    }

    else{
        reload_virusmove->stop();
        delete_reload_Virus(virus);
    }

}

void View::virusMov(){

    if(r_vir->y < 700){
        r_vir->y = r_vir->y + 3;
        r_vir->setPos(r_vir->x, r_vir->y);
        viruspodMoving(r_vir);
  }
}

void View::PlayerAttack(){

    player_bullet = new p_LaserBullet();
    player_bullet2 = new p_LaserBullet();

    player_bullet->x = pl.x + 46;
    player_bullet->y = pl.y - 33;

    player_bullet2->x = pl.x + 28;
    player_bullet2->y = pl.y - 33;

    player_bullet->setPos(player_bullet->x, player_bullet->y);
    mainScene.addItem(player_bullet);

    player_bullet2->setPos(player_bullet2->x, player_bullet2->y);
    mainScene.addItem(player_bullet2);

    p_Bullets.append(player_bullet);
    p_Bullets.append(player_bullet2);

    p_bulletpodMoving(player_bullet, player_bullet2);
}

void View::p_bulletpodMoving(QGraphicsItem *bullet_1, QGraphicsItem *bullet_2){

    if(player_bullet->y + 5 > 3 && player_bullet2->y + 5 > 3){
        player_bulletmove->start(20);
    }

    else {
        player_bulletmove->stop();
        delete_player_bullet(bullet_1, bullet_2);
    }
}

void View::p_bulletMov(){

    if(player_bullet->y + 5 > 3 && player_bullet2->y + 5 > 3){
        player_bullet->y = player_bullet->y - 5;
        player_bullet2->y = player_bullet2->y - 5;
        player_bullet->setPos(player_bullet->x, player_bullet->y);
        player_bullet2->setPos(player_bullet2->x, player_bullet2->y);
        p_bulletpodMoving(player_bullet, player_bullet2);
    }
}

void View::delete_player_bullet(QGraphicsItem *bullet1, QGraphicsItem *bullet2){

    foreach(QGraphicsItem *b, p_Bullets){
        if(b == bullet1){
            mainScene.removeItem(b);
            delete bullet1;
            p_Bullets.removeOne(b);
            player_bullet->y = 0;
        }
    }

    foreach(QGraphicsItem *b, p_Bullets){
        if(b == bullet2){
            mainScene.removeItem(b);
            delete bullet2;
            p_Bullets.removeOne(b);
            player_bullet2->y = 0;
        }
    }
}

void View::addType1Enemy(){

    Type1_Enemy *enemy_1type = new Type1_Enemy();
    enemy_1type->setPos(enemy_1type->x, enemy_1type->y);
    mainScene.addItem(enemy_1type);
    T1_Enemy_Attack->start(1000);
}

void View::Type1_EnemyAttack(){

    T1_Enemy_bullet = new Type1_EnemyBullet();
    T1_Enemy_bullet2 = new Type1_EnemyBullet();

    T1_Enemy_bullet->x = Type1_Enemy_res->x + 20;
    T1_Enemy_bullet->y = Type1_Enemy_res->y + 105;
    T1_Enemy_bullet->setPos(T1_Enemy_bullet->x, T1_Enemy_bullet->y);

    T1_Enemy_bullet2->x = Type1_Enemy_res->x + 40;
    T1_Enemy_bullet2->y = Type1_Enemy_res->y + 105;
    T1_Enemy_bullet2->setPos(T1_Enemy_bullet2->x, T1_Enemy_bullet2->y);

    mainScene.addItem(T1_Enemy_bullet);
    mainScene.addItem(T1_Enemy_bullet2);

    e1_Bullets.append(T1_Enemy_bullet);
    e1_Bullets.append(T1_Enemy_bullet2);

    T1_E_bulletpodMoving(T1_Enemy_bullet, T1_Enemy_bullet2);

}

void View::T1_E_bulletpodMoving(QGraphicsItem *bullet_1, QGraphicsItem *bullet_2){

    if(T1_Enemy_bullet->y < 697 && T1_Enemy_bullet2->y < 697){
        T1_Enemy_bulletmove->start(20);
    }

    else{
        T1_Enemy_bulletmove->stop();
        delete_T1_E_bullet(bullet_1, bullet_2);
    }
}

void View:: T1_E_BulletMov(){

    if(T1_Enemy_bullet->y < 697 && T1_Enemy_bullet2->y < 697){

        T1_Enemy_bullet->y = T1_Enemy_bullet->y + 5;
        T1_Enemy_bullet2->y = T1_Enemy_bullet2->y + 5;
        T1_Enemy_bullet->setPos(T1_Enemy_bullet->x,T1_Enemy_bullet->y);
        T1_Enemy_bullet2->setPos(T1_Enemy_bullet2->x, T1_Enemy_bullet2->y);
        T1_E_bulletpodMoving(T1_Enemy_bullet, T1_Enemy_bullet2);
    }

}

void View::delete_T1_E_bullet(QGraphicsItem *bullet_1, QGraphicsItem *bullet_2){

    foreach(Type1_EnemyBullet *b, e1_Bullets){
        if(b == bullet_1){
            mainScene.removeItem(b);
            delete bullet_1;
            e1_Bullets.removeOne(b);
            T1_Enemy_bullet->y = 0;
        }
    }

    foreach(Type1_EnemyBullet *b, e1_Bullets){
        if(b == bullet_2){
            mainScene.removeItem(b);
            delete bullet_2;
            e1_Bullets.removeOne(b);
            T1_Enemy_bullet->y = 0;
        }
    }
}


view.h :

#pragma once

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QObject>

#include "player.h"
#include "type1_enemy.h"
#include "reload_virus.h"
#include "p_laserbullet.h"
#include "type1_enemybullet.h"

class View :public QGraphicsView
{
public:
    View();
    void view_elements();
    QList<reload_Virus*> Viruses;
    QList<QGraphicsItem*> p_Bullets;
    QList<Type1_EnemyBullet*> e1_Bullets;
    void viruspodMoving(QGraphicsItem *virus);
    void delete_reload_Virus(QGraphicsItem *virus);
    void delete_player_bullet(QGraphicsItem *bullet1,QGraphicsItem *bullet2);
    void p_bulletpodMoving(QGraphicsItem *bullet_1, QGraphicsItem *bullet_2);
    void delete_T1_E_bullet(QGraphicsItem *bullet_1, QGraphicsItem *bullet_2);
    void T1_E_bulletpodMoving(QGraphicsItem *bullet_1, QGraphicsItem *bullet_2);
    reload_Virus *r_vir;
    Player pl;
    Type1_Enemy *Type1_Enemy_res;

public slots:
    void addVirus();
    void virusMov();
    void p_bulletMov();
    void T1_E_BulletMov();
    void PlayerAttack();
    void Type1_EnemyAttack();
    void addType1Enemy();

private:
    QGraphicsScene mainScene;
    p_LaserBullet *player_bullet;
    p_LaserBullet *player_bullet2;
    Type1_EnemyBullet *T1_Enemy_bullet;
    Type1_EnemyBullet *T1_Enemy_bullet2;
    View *view;
    QTimer     *startGame; // Начало игры
    QTimer *create_reload_Virus;
    QTimer *reload_virusmove;
    QTimer *player_bulletmove;
    QTimer *T1_Enemy_bulletmove;
    QTimer *T1_Enemy_Attack;
    QTimer *create_type1_enemy;

};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2021-04-23
@jcmvbkbc

How to fix?

Read the error message first. Understand that it says that the heap is broken. By the power of thought, find a place in the program that breaks it. If the power of thought does not work, then compile the program with some useful debugging options, run and analyze the output. For gcc it would be an option -fsanitize=address, what M$ has for that I don't know.

W
Wataru, 2021-04-23
@wataru

Judging by the error, you delete something selected through new through delete and then use it anyway.
I see at least one such place. delete_reload_virus sometimes removes virus and then does something with r_vir. But as an argument, the same r_vir is passed to it from viruspodMoving.
Some kind of mess is going on there. The same variables are used as class members and are passed as pointers throughout the program. The devil's leg will break there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question