我有一个 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()
如果需要任何额外的代码,请告诉我,谢谢。