Jump to content
  • Advertisement

Archived

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

MeshMan

CList : Deleting when Iterating

This topic is 5224 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!