In my simple game engine I have a vector containing pointers to entities. Each entity is allocated with the new keyword. To call functions on my entities I use an iterator and so when deleting my entities I figured I'd use an iterator as well, like so:
vector<Entity*>::iterator iter;
for (iter = gameEntitys.begin(); iter != gameEntitys.end();) {
delete (*iter);
++iter;
}
But I now have a horrific memory leak and all profiling tools point towards the line delete (*iter);
Clearly there is something wrong but what is the correct way for me to delete the entities contained in the vector (and clear the vector for another scene). Using gameEntitys.clear();
is useless to my knowledge as it only removes the elements of the vector not actually calling delete on them as they are pointers rather than actual data.
Edit: I have bee watching the comments. The Entity class is not polymorphic and does not have any subclasses. Would it make more sense if I stopped using dynamic memory and had a normal array of non-pointer entities?
The reason I know there is a memory leak is because when the application starts the memory use shoots up to more than 2gb before crashing.