I had a vector<Points*> points;
(size: 6
with all unique Points
) in my program wherein I was iterating through the points to draw something on the screen. However, as per my new requirements, I'd to increase the length of the vector to size: 14
.
The new items that were to be added had to be from the previous 6
Points
, so instead of allocating new memory, I thought of just using the previous pointers as follows:
while (currentSize < 14){
int rndPoint = getRandomPoint(0, 5); //random index to choose from the vector
points->push_back(points[randPoint]);
}
In the destructor of the class, when I've to deallocate the memory, I'm doing the following:
for(int i=0;i<points.size(); ++i){
if(points[i] != NULL){
delete (points[i]);
}
}
However, when I try to exit the program - I'm getting an access violation error in the loop (specifically when i
reaches index 6
). When I've already deleted the 6 unique points by using delete
, why is the condition if (points[i] != NULL)
resulting in true
for i=6,7...13
?