I have recently started writing an engine in C++ / SDL for a horizontal scrolling shooter, and most of the functionality that I wanted for it is done.
Now I am trying to deal with the memory leaks, and with that I would appreciate some help.
I store my particles, stars, bullets, enemies in my World object, that has vectors for them to be contained, such as these:
vector<Enemy> v_enemies; vector<Enemy>::iterator v_enemies_it; vector<Particle> v_particles; vector<Particle>::iterator v_particles_it;
new objects to these vectors are added in several different functions in the following way:
Particle a(xx, yy, 6); v_particles.push_back(a);
when the vectors are dead and flagged to be deleted, I erase them from the vector with remove-erase idiom in the following way:
v_particles.erase(remove_if(v_particles.begin(), v_particles.end(), dead_p), v_particles.end());
I do understand, that the memory leak is because the fact that the vector.erase only deletes the pointer to the object, and not the object itself, however when I tried to step through the vector with its iterator manually in a loop, I have encountered crashes due to the fact that the vector iterator is invalidated when the vector is reallocated.
I would really appreciate it if someone could provide me a method of erasing dead object from the vector, in a way that the object itself is deleted, not only its pointer. Also, I suspect my way of adding the object to the vector might not be the best way either (should I use "new"?).
I hope I have not broken any forum rules. This is my first post, so please go easy on me .
Thanks in advance!
(btw here is a short clip of the game engine so far: http://www.youtube.com/watch?v=EjaTcaEIQSU)