In my C++ game I have an array of pointers to EnemyTypes. I used Visual Leak Detector to detect memory leaks, and it's telling me I have a leak in the following piece of code:
vector<EnemyType*> enemyTypes(number_of_lines);
for (int i = 0; i < number_of_lines; i++)
{
enemyTypes[i] = new EnemyType();
}
Let's say number_of_lines is 3 in this case. How is it possible that I'm creating a leak right here? Can I do something about it?
I started learning C++ about a month ago and I'm still learning every day, but I cannot understand some things (like this) without someone explaining me.
EDIT: I have modified the code to use a vector instead of a plain array.
enemyTypesis local to a method and you don'tdeleteeach element (and the array itself) then you have a leak. Consider using astd::vector<EnemyType>, dynamic memory is not always desiderable, and when used, proper smart pointers could leverage the management part.