【问题标题】:SFML destructor issues with deleting array删除数组的 SFML 析构函数问题
【发布时间】:2015-08-17 19:01:02
【问题描述】:

我一直在研究我的 spaceInvaders 克隆,现在我正在尝试通过清理内存泄漏来完成该项目。

目前我正在尝试删除在构造函数中创建的 11 个 aliensobjects 数组,但是在这样做的同时,程序会在 AlienRow 的析构函数处中断(崩溃)。

我尝试了以下方法:

在构造函数中创建空指针并用这个删除:

for (int i = 0; i < 11; i++)
    {
        if (alienRow[i] != nullptr)
        {
            delete alienRow[i];
        }
         delete *alienRow;

以及: delete [] alienRow;

任何关于为什么会发生此问题的指针?

#include "AlienRow.h"

AlienRow::AlienRow(float x, float y, int type){
    for (int i = 0; i < 11; i++)
    {
        alienRow[i] = nullptr;
    }
    if (type == 1){

        for (int i = 0; i < 11; i++)
        {
            alienRow[i] = new Alien(x, y, "Alien1.png");
            x = x + 70;
        }
    }
    if (type == 2){

        for (int i = 0; i < 11; i++)
        {
            alienRow[i] = new Alien(x, y, "Alien2.png");
            x = x + 70;
        }
    }
    if (type == 3){

        for (int i = 0; i < 11; i++)
        {
            alienRow[i] = new Alien(x, y, "Alien3.png");
            x = x + 70;
        }
    }

}

AlienRow::~AlienRow(){
    /*for (int i = 0; i < 11; i++)
    {
        if (alienRow[i] != nullptr)
        {
            delete alienRow[i];
        }
         delete *alienRow;
    }*/

    delete [] alienRow;
}


void AlienRow::draw(RenderTarget& target, RenderStates states)const{

    for (int i = 0; i < 11; i++)
    {
        target.draw(*alienRow[i]);
    }


}

Alien* AlienRow::getAlienRowA(int nr)const{
    return alienRow[nr];
}


bool AlienRow::getAlienMove(float x, float y){
    for (int i = 0; i < 11; i++)
    {
        if (alienRow[i]->moveAlien(x, y) == true)
            return true;
    }
    return false;
}



#pragma once
#include "Alien.h"
#include <iostream>


class AlienRow :public sf::Drawable {

private:
    Alien* alienRow[11];

    float alienVelocity;

public:
    Alien* getAlienRowA(int nr)const;
    virtual void draw(RenderTarget& target, RenderStates states)const;

    bool getAlienMove(float x, float y);
    AlienRow(float x, float y, int type);
     ~AlienRow();
};

外星人:

#include "Alien.h"
#include <iostream>

Alien::Alien(float x, float y, std::string alien){

    alienTexture.loadFromFile(alien);
    alienSprite.setTexture(alienTexture);
    alienSprite.setPosition(x, y);
    alienSprite.setScale(sf::Vector2f(0.7f, 0.7f));


}

Alien::~Alien(){

}


void Alien::draw(RenderTarget& target, RenderStates states)const{

    target.draw(alienSprite);
}

void Alien::update(float dt){

}

Sprite Alien::getAlienSprite()const{
    return alienSprite;
}

void Alien::moveDeadSprite(){
    alienSprite.setPosition(alienSprite.getPosition().x, alienSprite.getPosition().y - 700);
}

bool Alien::moveAlien(float x, float y){
    alienSprite.move(x, y);
    if (alienSprite.getPosition().y > 540){
        return true;
    }

    return false;

}

#pragma once
#include "Entity.h"

class Alien:public Entity{

private:
    Sprite alienSprite;
    Texture alienTexture;

    float velocity;

public:
    Sprite getAlienSprite()const;

    void moveDeadSprite();
    bool moveAlien(float x, float y);

    virtual void draw(RenderTarget& target, RenderStates states)const;
    virtual void update(float dt);
    Alien(float x, float y, std::string alien);
    virtual ~Alien();



};

外星人

#include "AlienSwarm.h"

AlienSwarm::AlienSwarm(float y){

        aRow[0] = new AlienRow(0,y,3);
        y = y + 50;
        aRow[1] = new AlienRow(0,y,2);
        y = y + 50;
        aRow[2] = new AlienRow(0, y,3);
        y = y + 50;
        aRow[3] = new AlienRow(0, y,2);
        y = y + 50;
        aRow[4] = new AlienRow(0, y,1);
        y = y + 50;

}

AlienSwarm::~AlienSwarm(){
    for (int i = 0; i < 5; i++)
    {
        delete aRow[i];
    }
}


void AlienSwarm::draw(RenderTarget& target, RenderStates states)const{

    for (int i = 0; i < 5; i++){

        target.draw(*aRow[i]);
    }

}

AlienRow* AlienSwarm::getAlienSwarmA(int nr)const{
    return aRow[nr];
}

void AlienSwarm::update(){


}

bool AlienSwarm::getAlienMoveRow(){
    for (int i = 0; i < 5; i++)
    {
        if (aRow[i]->getAlienMove(0, 0.5f) == true)
            return true;
    }
    return false;
}

#pragma once

#include "AlienRow.h"

class AlienSwarm:public Drawable{

private:
    AlienRow* aRow[5];
    float y;
public:
    AlienRow* getAlienSwarmA(int nr)const;

    bool getAlienMoveRow();

    virtual void draw(RenderTarget& target, RenderStates states)const;
    virtual void update();
    AlienSwarm(float y);
    virtual ~AlienSwarm();



};

【问题讨论】:

    标签: c++ arrays sfml delete-operator


    【解决方案1】:

    将您的 new 呼叫与您的 delete 呼叫相匹配。

    delete *alienRow;
    

    没有匹配的new。您有效地删除了数组的第一个元素 12 次。删除此行。

    delete [] alienRow;
    

    您的数组不是使用new 创建的。也删除此行。

    【讨论】:

      【解决方案2】:

      使用智能指针和(unique_ptr 或 shared_ptr),您不必删除它们。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-16
        • 2012-08-11
        • 2016-03-14
        • 2015-12-31
        • 2013-12-14
        • 2013-04-27
        • 2012-03-13
        • 2017-04-06
        相关资源
        最近更新 更多