I'm currently working on a game as a project for my University. It's being made in C++ with SDL2.
I have a vector that holds pointers of the class Enemies
, which is an abstract parent class of the Plant
class. In the constructor of my enemy manager, I am pushing back a pointer to a Plant
object into the enemies vector.
m_pEnemies.push_back(new Plant(projectileManager));
At some point, the plant comes in contact with a projectile (the detection works fine and all), and I run this piece of code in order to remove it from the vector:
Enemies* temp{ m_pEnemies[i] };
m_pEnemies[i] = m_pEnemies.back();
m_pEnemies.back() = temp;
delete m_pEnemies.back();
m_pEnemies.pop_back();
The enemy is destroyed during the game, and I am getting no run-time errors for illegal memory accessing, but there are memory leaks. When I placed a breakpoint, it showed that the destructor of the Plant
object does not get called. It should be when I pop the back of the vector, but for some reason it doesn't.