Jump to content
  • Advertisement
Sign in to follow this  
Mantear

[C++] std::list reverse iterator

This topic is 3720 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

Greetings, I have a short snippet of code that just isn't working for me. I have a feeling I'm doing something really stupid but I just can't see it. A free cookie to the first one who points out what I've done wrong! When the code crashes, I have two elements in the list. It steps through the loop twice, hitting each element, but then it attempts to decrement once more (incrementing the reverse iterator) and I get this error: "Debug Assertion Failed! ... Expression: list iterator not decrementable"
    // Have the widgets' pop-ups process the event.
    for (std::list<DialogWindow*>::reverse_iterator iter = m_DialogWindows.rbegin();
         iter != m_DialogWindows.rend();
         ++iter)
    {
		if ((*iter)->ProcessPopupLClick(mouse_x, y))
        {
            processed = true;
        }
    }

Share this post


Link to post
Share on other sites
Advertisement
Bah, never mind. I found the reason. The call to ProcessPopupLClick() can do a call-back that re-orders the list, making the current iterators invalid. I'll need to find another way...

Share this post


Link to post
Share on other sites
Quote:
Original post by chipmeisterc
You might want to make sure that (*iter) isnt NULL also :)


Every DialogWindow instance that is created is immediately put into this list and only ever delete-ed upon the destructor call to the class containing the list. So, it's pretty safe for me to not do the check (currently). However, you're right, it's probably a good idea to put the safetly check in anyways, in case stuff changes later.

Share this post


Link to post
Share on other sites
Another interesting item that I've recently discovered for myself. I put in the guards needed to keep the list from being re-ordered while it is being iterated over. However, due to some left-over debug code, it was still crashing as shown in the example code below.
std::list<int> mylist;

mylist.push_back(4);
mylist.push_back(3);
mylist.push_back(2);
mylist.push_back(1);
mylist.push_back(0);

for (std::list<int>::iterator iter = mylist.begin(); iter != mylist.end(); ++iter)
{
if (*iter == 2)
{
ReOrderList(&mylist);
std::list<int>::iterator iter2 = iter; // <------ CRASH
break;
}
}

The 'iter2' variable was a left-over debugging snippet that was causing the program to crash. What I don't understand is why. After ReOrderList() is called, 'iter' is no longer valid, but why does that mean I can't make a copy of it? I understand that I can't use 'iter' or its copy, 'iter2', but why does the mere copy of 'iter' crash?

Share this post


Link to post
Share on other sites
Because it's more useful that way. The more often an iterator is checked, the more narrowly the point at which it became invalid can be pinpointed. So iterator checking will happen with all possible functions, not just the ones that actually dereference it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!