Let's say we have a Buyer class.
Buyer has an email data member and a getEmail() member function.
Also we have a method like the one below that deletes a pointer to Buyer objects from a list of pointers to Buyer objects
void removeBuyer(Buyer* b){
list<Buyer*> :: iterator z;
for(z = buyersList.begin(); z != buyersList.end(); ){//buyersList is the list of pointers to Buyer objects
if( (*z)->getEmail() == b->getEmail() ){
z = buyersList.erase(z);
}
else
++z;
}
Then let's say I try to "log in" with the Buyer object whose pointer I just deleted.
void logIn{
cout<<"Give email"<<endl;
std::string e;
std::cin>>e;
list <Buyer*> :: iterator it;
for(it = buyersList().begin(); it != buyersList().end(); ++it){
if (e == (*it)->getEmail() ){// This is where the crash eventually occurs
//something
}
}
}
This sometimes works fine, others a crash occurs with the 0xC0000005 return.
I know that the buyersList (inside logIn) is updated and holds all the pointers minus the deleted one. And when I dereference the iterator it, it then "becomes" one of the elements of the list, hence a pointer that exists and is not deleted.
I know that I'm probably doing something wrong with the handling of the pointer I just deleted. What am I missing exactly?
The issue was that buyersList() returned by value and not by reference.
User contributions licensed under CC BY-SA 3.0