用C ++编写奇怪的析构函数

时间:2013-08-24 14:54:23

标签: c++

我有那些继承类: 基类:实体

派生自实体类:Actor,Obj,Enemy

基类实体包含一个用户定义类型的obj,我称之为“CollisionStuff”。 当我运行我的程序时,每次CollisionStuff构造函数调用之后都会调用CollisionStuff的析构函数,并且每次游戏循环都会继续。

所以我的电话是:为什么会发生这种情况?

正如你在下面看到的,我在setRectangle方法中分配一些数组,程序调用析构函数,删除我的数据,当我尝试使用它们时...它调用“_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse));”。

之前谢谢你

这里是我的代码: Entity.h

enum e_Type {tActor = 0, tObj, tEnemy, tBackg};

class Entity
{
public:
    Entity(void);
    ~Entity(void);

    float getH();
    float getW();
    void setWH(float W, float H);
    bool CreateSprite(std::string path);
    sf::Sprite& getSprite();
    void setType(e_Type type);
    e_Type getType();
    CollisionStuff getColStuff();

    static std::list<Entity*> List;

protected:
    sf::Sprite m_sprite;
    sf::Texture m_texture;
    float m_h;
    float m_w;
    e_Type m_type;
    CollisionStuff m_colStuff;

    void addToList();
};

CollisionStuff.h

class CollisionStuff
{
public:
    CollisionStuff();
    ~CollisionStuff(void);

    void setRectangle(int  W, int H);
    void followTheSprite(Entity entity);

private:
    sf::Vector2f* m_a;
    sf::Vector2f* m_b;
    sf::Vector2f* m_c;
    sf::Vector2f* m_d;
    /* this member data are sides of rectangle used
    to manage collisions between object throughout the scenario 

            a
      -------------
      |           |
    c |           | d
      |           |
      -------------
            b
    */

};

CollisionStuff.cpp

CollisionStuff::CollisionStuff()
{
    //setRectangle(0, 0);
}

void CollisionStuff::setRectangle(int W, int H)
{
    m_a = new sf::Vector2f[W];
    m_b = new sf::Vector2f[W];
    m_c = new sf::Vector2f[H];
    m_d = new sf::Vector2f[H];
}

void CollisionStuff::followTheSprite(Entity entity)
{
    entity.getSprite().setOrigin(0, 0);
    sf::Vector2f UpLeftVertex = entity.getSprite().getPosition();


    for(int i = 0; i <  entity.getW(); i++)
    {
        m_a[i].x = UpLeftVertex.x + i;
        m_a[i].y = UpLeftVertex.y;

        m_b[i].x = UpLeftVertex.x + i;
        m_b[i].y = UpLeftVertex.y + entity.getH();
    }

    for(int i = 0; i <  entity.getH(); i++)
    {
        m_c[i].x = UpLeftVertex.x;
        m_c[i].y = UpLeftVertex.y + i;

        m_d[i].x = UpLeftVertex.x + entity.getW();
        m_d[i].y = UpLeftVertex.y + i;
    }

}



CollisionStuff::~CollisionStuff(void)
{
    delete [] m_a;
    delete [] m_b;
    delete [] m_c;
    delete [] m_d;
}

修改 谢谢你的答案。 CollisionStuff使用示例

Actor.cpp (它是实体的派生类)

Actor::Actor(void)
{
    if(!CreateSprite("D://Sprites//MainChar.png"))
    {
        std::cout << "Impossibile creare sprite" << std::endl;
    }
    else
    {
        std::cout << "Creazione sprite riuscita" << std::endl;  
        m_sprite.setPosition(100.0f, 365.0f);
        m_sprite.setOrigin(20, 35);
        //m_sprite.setPosition(190.0f, 382.5f); // 200, 400
        setWH(40, 70);
        m_health = 100;
        m_status = Good;
        setType(tActor);
        m_jCounter = -1;
        m_action = Null;

        setColStuff();
    }
}

void Actor::setColStuff()
{
    m_colStuff.setRectangle(m_w, m_h);
}



void Actor::physic()
{
    //setColStuff();
    m_colStuff.followTheSprite(*this);
}

的main.cpp

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Platform");

    std::list<Entity*>::iterator i;

    Background BG;
    Level1 FirstLev;
    Actor Doodle; 

    while(window.isOpen())
    {
        sf::Event event;
        if(window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            Doodle.inputEvts();
        }

        Doodle.act(Doodle.getAction());

        Doodle.physic();

        window.clear();

        window.draw(BG.getSprite());
        window.draw(Doodle.getSprite());

        FirstLev.drawLevel(window);

        window.display();
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

很难从你发布的代码中分辨出来,但如果我不得不猜测我会说它可能与此有关:

CollisionStuff getColStuff();

您按值返回CollisionStuff,这意味着无论是谁调用此副本都会创建新副本。它将具有与原始CollisionStuff对象分配的相同的指针,并且当它超出范围时将删除它们,使原始指针悬挂指针。

您可以尝试通过引用或指针返回,但无论哪种方式,您都应该编写复制构造函数并覆盖CollisionStuffRule of Three)的赋值运算符。

另一个想法是使用std::vector<sf::Vector2f>而不是自己分配sf::Vector2f数组。