Archived

This topic is now archived and is closed to further replies.

MeshMan

CList : Deleting when Iterating

Recommended Posts

Hey all. I was wondering how you guys manage to stay valid inside a for loop when iterating over a list and deleting a node inside it. Take the following for example:
CList<object*> m_Objects;

...

for (int x = 0; x < m_Objects.Count(); x++)
{
    OBJECT *pObj = m_Objects.Get(x);
    ....
    m_Objects.Remove(pObj);   // List has changed now!
    ....
}

 

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you run backwards through the list, there should be no problem.

Share this post


Link to post
Share on other sites
That is what im doing but i seem to keep getting a crash the next time i iterate the list...

m_List.Remove(pObj);
x--;
continue;

Could there be anything im doing wrong?
Ive even exited the iteration until the next game frame when i remove an item, but next iterate, its corrupted...Something im overlooking or do you think its my list class thats bad?

Share this post


Link to post
Share on other sites
Its my own custom list class.
Ive overloaded operator[] which just calls a Get(int index); to return the CListNode which just auto-casts to the data you put in like:

class CListNode { operator T() { return m_Data; }

Something like that, i cant remember off hand.

Share this post


Link to post
Share on other sites