I am trying to create a maze class that can read an input stream that has the description of the maze and return a maze. However when I run a test with this given input stream:
20 10
####################
#................<.#
#..................#
#...###............#
#.....#............#
#.....#............#
#...###............#
#..................#
#..................#
####################
It gives a segmentation fault, and I run the object file on valgrind to check what is happening:
Invalid write of size 8
==2482545== at 0x4032CD: Maze::setTile(Position const&, Tile*) (maze.cpp:47)
==2482545== by 0x40347B: Maze::read(std::istream&) (maze.cpp:67)
.....
==2482545== Address 0x0 is not stack'd, malloc'd or (recently) free'd
I don't really understand why there is an segmentation fault or an invalid write, in my code, I should have allocated the space for each tile inside my setTile function, so there should be space for me to write. I also stacked the tile_collection with my constructor so tile_collection should be initialized when I called Maze(20,10), and the resizing inside setTile should be working. Can you please point out what am I missing? Thank you in advance. I know that I asked a really similar question 3 hours ago but I am still stuck on this invalid write.(The Position class and tilefactory class are 2 given files so they should work fine) This is the class declared in header file:
class Maze {
private:
// TODO: add fields
int Width;
int Height;
std::vector<Tile*> tile_collection;
This is my cpp file:
Maze::Maze(int width,int height):
Width(width),Height(height){
tile_collection[(width-1)*(height)];
}
}
void Maze::setTile(const Position &pos,Tile *tile){
tile_collection[pos.getX()+pos.getY()*(Width)]=tile;
}
Maze *Maze::read(std::istream &in){
int x;int y;char c;
if ((in>>x)&&(in>>y)){
Maze *new_maze=new Maze(x,y);
//loop over the specified maze dimension
for (int i=0;i<y;i++){
for (int j=0;j<x;j++){
if (in>>c){
//using tilefactory to change character into a tile
TileFactory *fac=TileFactory::getInstance();
Tile* temp=fac->createFromChar(c);
//if createFromChar fails, return nullptr, otherwise set tile at position j,i
if (temp==nullptr){
return nullptr;
}
else{
new_maze->setTile(Position(j,i),temp);
}
}
}
}
return new_maze;
}
else{
return nullptr;
}
}
I suspect that the fault you are seeing is a consequence of this line:
TileFactory *fac=fac->getInstance();
You're declaring a local pointer-variable named fac
but you are trying to call a method ( getInstance()
) through that same pointer-variable before it's even been set to anything. Seems like a recipe for disaster to me!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.