Re: Problem in deleting entries from Hash_set
On Mar 6, 7:58 pm, ZikO <ze...@op.pl> wrote:
Prafulla T wrote:
> for (hash_set<UserType*>::iterator hashIt = hashSet.begin(=
);
> hashIt != hashSet.end(); hashIt++)
> {
> UserType *pUserType = *hashIt;
> hashIt++;
> delete pUserType;
> pUserType = NULL;
> }
OK. First of all you cannot use hash_set.begin(). hash_set<> is the type
not instantiated object! If this worked, that code below would also work =
!!!
template <class T>
struct A {
void begin() {}};
int main() {
//! A.f(); // very baaaad :P
A<int> a;
a.f(); // correct
}
hashSet was the name of hash_set object.
It is not clear to me how it would cause the problem.
And yes, incrementing iterator twice was a problem. I think I put this
while writing the mail. it was not actually there in the code. (It was
not in "for loop" in actual code).
I debugged it further and found that cause was somewhere else.
Of course, i missed to clear the hash_set after deleting the elements.
It didn't cause any trouble in my case.
* you are trying to delete object pointed by pUserType, which has not
been created dynamically by operator new !!!
It was created dynamically by operator new. I just didn't add that
code here.
* you compare pointers of two different objects, UserType with container
hash_set !!!
??
Now having said that all, if I understand you, your code should look
like below (roughly):
// ...
hash_set<UserType*> hsutp; // it can be any name used by you
// I assume u hav=
e done something on
// this object !!=
!
// ...
for (hash_set<UserType*>::iterator hashIt = hsutp.begin();
hashIt != hsutp.end(); ++hashIt) {
delete *hashIt;
*hashIt = 0;
}
Best.