It is unclear who is responsible for managing the lifetime of the objects pointed by the pointers inside classes
. Have you pushed new
ed pointers into it, or have you pushed the addresses of automatic storage objects?
If you have done the former, then you must manually delete
the pointer before removing it. Else, if you have done the latter, then you could just leave it as is, just leaving the pointed-to objects destroy themselves as they leave their respective scopes. If you have mixed new
ed and non-new
ed pointers, whose possibility isn't that remote as you would think, then you're definitely damned, undefined behavior making demons fly out of your nose.
These kinds of situations involving pointers are very ambiguous, and it is generally recommended not to use pointers at all, and make the std::vector
store plain objects, which makes your object lifetime management much simpler and the making the declaration just speak for itself.
vector<MyClass> classes; // Do this instead