Most likely, the memory leak comes from the destructor of your Entity or one of its subclasses: you perform your delete operations correctly, so the destructor itself must be to blame. One common problem is not that destructors are virtual in the hierarchy of polymorphic classes.
You are also absolutely right about the futility of calling gameEntitys.clear() : it will leak your objects “wholesale” by “forgetting” the references to them.
If you are in C ++ 11, consider changing the definition to use std::unique_ptr
std::vector<std::unique_ptr<Entity> > gameEntitys;
This eliminates the need to manually manage the memory of your records, allowing you to hold pointers to objects of derived classes in vector . If you use unique_ptr , calls to gameEntitys.clear() will destroy the elements that the vector elements point to.
Working with the unique_ptr vector unique_ptr slightly different (for example, extra care is required to insert new elements, see this answer ), but I think the positives of simplified memory management compensate for the slight inconvenience.
EDIT:. Since your Entity class is not polymorphic, consider switching to std::vector<Entity> if you do not plan to move to a polymorphic hierarchy later.
source share