I've been teaching myself C++ in VS2012 and have come across something that truly has gotten me scratching my head. When creating an object and adding a reference to it in another, the object seems to get corrupted or something. It's not null (every combination of null pointer check I could make confirms this), but its contents are funky and trying to access one of its members results in "0xC0000005: Access violation reading location 0xCCCCCCE8".
The gist of my program is that I have a 2D vector of Tile objects, each of which may have a reference to a Site. When I'm in a tile's draw() function that has a Site, it will attempt to draw that site in the colour that its civilisation belongs to.
World generation:
std::vector<std::vector<Tile>> world;
// ... vector is sized ...
// Populate world with tiles
for (int h=0; h<height; h++) {
for (int w=0; w<width; w++) {
Tile t(PLAINS);
world[h][w] = t;
}
}
When a civilisation is created, a simple capital site is created for them and a reference to it put in the tile at the specified x,y:
void Game::createCivilisation(std::string name, sf::Color colour, int x, int y) {
// Create the civ
Civilisation civ(name, colour);
// Create their capital and link it up
Site capital("Capital", year, &civ); // when I comment these five lines out
civ.capital = &capital; // the program doesn't crash, so I know
civ.sites.push_back(capital); // that the dodgy site originates here.
// Add capital to tile
world[y][x].site = &capital;
capital.location = &world[y][x];
}
By the time the Tile's draw() function is called however, it dies upon trying to access the site's members. This is with just the one civilisation.
if (site != nullptr) { // have also tried (site) (site!=0) (site!=NULL)
sf::CircleShape s(tileWidth*0.5);
s.setFillColor(site->civilisation->colour); // womp womp
The image above is the debug info supplied. As you can see, it's all rubbish. "Capital" has become some billion-long string of garbage, everything is wrong, all the references to other objects are gone too. When I comment out the capital site linking
I think I've supplied all the relevant snippets of code (in the interest of brevity / for her pleasure). I've been working on this one for a few hours and it has me stumped. All indices should be correct, the site generated in createCivilisation() is definitely the only site in existence, etc...