Re: Problem in deleting entries from Hash_set
Prafulla T <prafulla.tekawade@gmail.com> wrote in news:3e695b9b-1c2c-4416-
ba28-9c21337ee0ba@p6g2000pre.googlegroups.com:
Is following correct way of deleting entries from stl hashset.
Will it create any memory corruption ?
One of my program uses this way and it is crashing in strange way but
purify or valgrind does not
show up anything. I am suspecting this piece of code as a problem.
Can anyone comment on it ?
for (hash_set<UserType*>::iterator hashIt = hashSet.begin();
hashIt != hashSet.end(); hashIt++)
{
UserType *pUserType = *hashIt;
hashIt++;
delete pUserType;
pUserType = NULL;
}
Hmmm, does your hash_set::erase() perhaps return an iterator? If so, the
loop you want probably looks something like.
for (hash_set<UserType *>::iterator hashIt = hashSet.begin();
hashIt != hashSet.end();)
{
UserType *pUserType = *hashIt;
hashIt = hashSet.erase(hashIt);
delete pUserType;
}
That should delete every item in your hash_set and delete the elements as
well.
HTH
joe