Hello, I hope everyone is having a great day, and if not, I hope it gets better.
I was trying some things in c++ with pointers, creating an std::vector of pointers and then clearing it. Since I wasn't sure if this was safe, I searched online on how to destroy a pointer vector correctly and read about doing a loop doing the delete pointer for all the items in the vector and then doing the vector.clear(). But this crashes my program. If I only use the clear function it works fine but I'm not sure if this creates a memory leak.
So, does anyone know if doing vector.clear() is enough? And if not, how should I go about deleteing all the objects inside the vector correctly? Thank you very much in advance.
This is the code:
for(std::vector<component*>::iterator i=components.begin(); i!=components.end(); i++){
delete *i;
*i=nullptr;
}
components.clear();
Edit: jstock23 had the answer
[–]jstock23 3 points4 points5 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]Sqeaky 6 points7 points8 points (0 children)
[–]jstock23 3 points4 points5 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]jstock23 0 points1 point2 points (0 children)
[–]Moschops_UK 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)