So, I have a class
class Room {
public:
Room();
~Room();
tile::Tile* tile[11][7]; // Owned
}
The has a constructor and destructor, tile::Tile
is an abstract base class, so is a pointer. The array of pointers tile, need to be populated in the constructor like this.
Room::Room() {
for (std::size_t i = 0; i < 11; ++i) {
for (std::size_t j = 0; j < 7; ++j) {
this->tile[i][j] = new tile::Empty();
}
}
}
From my understanding, I should also delete these in Room
's destructor.
Room::~Room() {
for (std::size_t i = 0; i < 11; ++i) {
for (std::size_t j = 0; j < 7; ++j) {
delete this->tile[i][j];
}
}
}
However, doing this results in a return code of 0xc0000374
, which is a heap corruption error. Why is this corruption error happening?
Minimum example
class Tile {};
class Empty: public Tile {
public:
Empty() {}
};
class Room {
public:
Tile* tiles[5];
Room() {
for (int i = 0; i < 5; ++i) {
tiles[i] = new Empty();
}
}
~Room() {
for (int i = 0; i < 5; ++i) {
delete tiles[i];
}
}
};
class Maze {
public:
Room rooms[5];
Maze() {
for (int i = 0; i < 5; ++i) {
rooms[i] = Room();
}
}
};
int main() {
Maze maze = Maze();
}