Using SFML, I had a Board class which held multiple vectors of all of my object types in the game, and then it also held a vector of pointers to the memory addresses of these object instances, like this
1 2 3 4 5 6 7 8 9 10 11
class Board{
//...
std::vector<AbstractObject*> GetAllLevelObjects(){ return allLevelObjects; }
//so these are used to hold my object instances for each level
std::vector<ObjectOne> someObjects;
std::vector<ObjectTwo> moreObjects;
/*this is used to easily loop through all my objects and draw them based on if
they're in the background or wherever*/
std::vector<AbstractObject*> allLevelObjects;
};
When looping through this vector and drawing the sprites of the objects, I get the runtime error 0xC0000005: Access violation reading location 0x00277000.
.
I solved this error by storing the vector of pointers in the class that holds my Board instance, but I'm wondering why only this solution worked? Why couldn't I just have my vector of pointers in the same class that the instances of those objects were in?
0xC0000005 is an invalid memoty access error. Usually it happens when you are trying to dereference invalid pointer. Are you sure that all pointers are valid? That no reallocation.deleting objects took place?
This vector of pointers is going to be both a maintenance nightmare and a performance bottleneck.
To preserve sanity, what would you have to do when one of these vectors reallocates memory?
Consider something like this instead: