I have a std::vector
array (vectorArr
) of objects. Have 2 threads, 1 push objects vectorArr
array and other work with them. Some objects must be deleted from it when I end work with them. So I just add such objects index number to other std::vector<int>
(DeleteList
) and then iterate over it and erase objects from vectorArr
by the saved index, but some times I'm getting an out of range exception. I don't understand why that happens.
std::vector<int> DeleteList;
int index = 0;
for (auto &data : vectorArr)
{
if (data.frames < 150) {
data.frames++;
//some code here
}
else DeleteList.push_back(index);
index++;
}
if (DeleteList.empty())
return;
for (auto &dIndex : DeleteList) {
if (dIndex < vectorArr.size())
vectorArr.erase(vectorArr.cbegin() + dIndex); // out of range exception
}
DeleteList.clear();