I am wondering what the best way is to clear a vector of vectors of raw pointers that point to objects owned by unique_ptrs without any memory leaks. To make this more concrete, suppose that I have:
std::vector<std::unique_ptr<Person>> people;
std::vector<std::unique_ptr<Animal>> animals;
std::vector<std::vector<Animal*>> animalOwnerships;
I know I can use people.clear() and animals.clear(), which will delete the unique pointers to the class objects, which in turn will delete the class objects themselves (please correct me if I'm mistaken here). If I call these first, then does animalOwnerships consist of dangling pointers? Is it okay to call animalOwnerships.clear() at this point without needing to worry about any memory leaks, or should it be called first, before the other two?
Also, do the individual vectors in animalOwnerships need to be cleared individually?
Thank you!
Aucun commentaire:
Enregistrer un commentaire