1

我有一个 2D 平铺指针向量。每次我加载不同的地图时,我想删除矢量中的所有瓷砖,释放内存,然后用不同的瓷砖重新填充它。

std::vector<std::vector<Tile*>> map;

这是我的瓷砖课程:

#ifndef _TILE_H
#define _TILE_H

#include <SFML\Graphics.hpp>

class Tile
{

public:
    int type;
    int color;
    int light_level;
    int loc_x, loc_y;

    Tile();
    ~Tile();

    sf::Sprite baseSprite;

    void Draw(int x, int y, sf::RenderWindow* rw);

    void setTexture(sf::Texture& texture);
};

#endif

我像这样填充向量:

map[x][y] = tile;

问题是,每次我用 Tiles 重新填充向量时,我的内存都会继续填满,直到游戏崩溃。我的问题是,如何正确删除向量中的所有内容以释放内存?

这是我尝试删除未释放内存的图块:

for(int ii = 0; ii < this->map.size(); ii++){
    for(int jj = 0; jj < this->h; jj++){

        delete &this->map[ii][jj]->baseSprite;
        delete &this->map[ii][jj]->color;
        delete &this->map[ii][jj]->light_level;
        delete &this->map[ii][jj]->loc_x;
        delete &this->map[ii][jj]->loc_y;
        delete &this->map[ii][jj]->type;

        delete &this->map[ii][jj];
    }
}


map.clear() 

如果需要任何额外的代码,请告诉我,谢谢。

4

1 回答 1

1

从类来看,使用指针而不是直接在向量中使用 sTile似乎很愚蠢。这是您的问题的可能解决方案:TileTile

  1. 定义一个复制构造函数,即定义Tile::Tile(const Tile&)应该做什么(它应该将所有数据成员复制到新构造的图块)。
  2. 定义一个赋值运算符,即定义Tile& Tile::operator=(const Tile&)应该做什么(它应该调用operator=其所有数据成员和的所有函数return *this)。

现在乍一看这似乎没问题,但我查看了sf::Sprite文档,并没有为sf::Sprite. 似乎精灵只能通过sf::Texture. 幸运的是,sf::Spriteeveryone 公开了一个返回自己的 bound 的公共成员函数sf::Texture。但是,查看 的文档sf::Sprite::getTexture(),它返回一个指向 的指针,如果还没有纹理绑定到,sf::Texture则可能是这样。您应该在复制构造函数和赋值运算符中检查.NULLsf::SpriteTile

那么,复制构造函数应该如下所示:

Tile::Tile(const Tile& other)
: type(other.type)
, color(other.color)
, light_level(other.light_level)
, loc_x(other.loc_x)
, loc_y(other.loc_y)
, baseSprite()
{
    sf::Texture* result = other.baseSprite.getTexture();
    if (result)
    {
        baseSprite.setTexture(*result);
    }
}

并且赋值运算符应该是这样的:

Tile& Tile::operator = (const Tile& other)
{
    type = other.type;
    color = other.color;
    light_level = other.light_level;
    loc_x = other.loc_x;
    loc_y = other.loc_y;
    sf::Texture* result = other.baseSprite.getTexture();
    if (result)
    {
        baseSprite.setTexture(*result);
    }
    return *this;
}

不仅如此,它似乎sf::Sprite也可以由纹理的子矩形定义,并且它可能具有您需要注意的更多属性。例如,ansf::Sprite还具有用于其位置、旋转、全局颜色的 getter 和 setter,您需要复制所有这些。无论如何,一旦完成,您就可以忘记指针混乱并实例化

std::vector<std::vector<Tile> >

反而。

于 2014-06-06T18:03:21.857 回答