# [C++] std::list reverse iterator

## Recommended Posts

Mantear    251
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 on other sites
SnotBob    202
I do not see anything wrong with that snippet. Is there any chance of memory corruption?

##### Share on other sites
Mantear    251
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 on other sites
chipmeisterc    268
You might want to make sure that (*iter) isnt NULL also :)

##### Share on other sites
Mantear    251
Quote:
 Original post by chipmeistercYou 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 on other sites
Mantear    251
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 on other sites
Sneftel    1788
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.