*EDIT : I removed the else section. The iterator was iterated twice *
Errors when running :
File: c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector Line: 99
Expression: vector iterator not incrementable
File: c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector Line: 100
Expression: "Standard C++ Libraries Out of Range" && 0
My code has to send alarms when it finds a new picture in a directory. But as I cannot delete the files by myself I had to trick. Before sending an alarm when I find an image, I compare it with pictures already sent(sentPictures). And in sentPictures I delete the images erased in the directory, because if I have to compare each time with every image that were found in the past, the program might be a bit heavy xD (it's in an infinite loop)
This is were I get the error. To find if the stored elements are still in the directory, I compare the old elements vector (sentPictures) with the new one (refreshVector). For every old element (sentPictures), if they are not found in the refreshVector, I delete them one by one. In my case I get the error even if nothing is done. Just walking through this loop crashes. In my case I have 9 elements in the directory, it prints 5 and crashes. I checked and there are as nuch elements in sentPictures then in refreshVector. I imagine I do not handle well the use of iterators. Here is the piece of code.
for(std::vector<Picture>::iterator it = sentPictures.begin(); it != sentPictures.end(); it++){
std::cout << "1" << std::endl;
if(std::find(refreshVector.begin(), refreshVector.end(), *it) == refreshVector.end()){
tcout << *it <<std::endl;
//if picture not found in refresh vector, then delete it
//delete *it;
//it = refreshVector.erase(it);
}else{
std::cout<< "2" <<std::endl;
++it;
}
}
PS : the instruction delete * it; doesn't compile. VS says "expression must have pointer or handle type", underlying "*" symbol. I'm gonna search if it isn't about the fact "it" is used in the if condition.
The instruction delete * it; doesn't compile.
You are trying to delete an object, delete takes a pointer. You also should NEVER delete/delete[] anything was not allocated using new/new[], which seems you are not since you have a vector with objects ( not pointers).
If you want to remove sth from a container you should use its appropriate function vector<T>::erase(). delete is only for objects allocated on the heap.
Also, in case of a vector you don't have to use an iterator you can safely use an index.
for(int i=0; i != sentPictures.size(); i++)
{
sentPictures[i]; //
//etc
}
Though a find function will still return an iterator.
Another note, don't post-increment an iterator, it will do a redundant copy, pre-increment it instead, though am not sure if the compiler will optimize that.
Now the erase problem seems to be ruled, I need to go through this code until its end. It crashes somewhere in the lopp but don't know why.
It crashes because you're erasing inside the loop; the array size is changed when removing an element, and the iterator will be invalid after the remove operation. Removing elements from a vector is inefficient, you may want to use a std::set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With