Std :: map :: iterator resets the program by increment

What could be causing this?

Here's the stack trace:

#0  0x0645c0f5 in std::_Rb_tree_increment (__x=0x83ee5b0)
    at ../../../../libstdc++-v3/src/tree.cc:69
#1  0x0805409a in std::_Rb_tree_iterator<std::pair<std::string const, Widget*> >::operator++ (
    this=0xbffff144)
    at /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/stl_tree.h:192
#2  0x08053d32 in Generic::StartLayout (this=0x8287d68) at Generic.cpp:195
#3  0x0804f6e1 in LCDControl::ConfigSetup (this=0xbffff26c) at LCDControl.cpp:91
#4  0x0804ed7c in LCDControl::Start (this=0xbffff26c, argc=1, argv=0xbffff404) at LCDControl.cpp:21
#5  0x08050964 in main (argc=1, argv=0xbffff404) at Main.cpp:11

      

And here's the code:

    for(std::map<std::string,Widget *>::iterator w = widgets_.begin();
        w != widgets_.end(); w++){
        if( w->second->GetType() & WIDGET_TYPE_BAR) {
            w->second->SetupChars();
        }
        w->second->Start();

    }

      

Edit: This next issue is related, so I won't open a completely new question. I'll leave the acceptance of the answer as it is. I just need to know something. I have two iterators, one in the main and one in this main after the function call. They both belong to the same card. Well, everything is damaged inside and inside and the main loop stops iterating.

Here's the code.

Here's StartLayout:

void Generic::StartLayout() {
    Error("StartLayout: %s", key.c_str());
    for(std::map<std::string,Widget *>::iterator w = widgets_.begin();
        w != widgets_.end(); w++){
        Error("Starting widget %s", w->first.c_str());
        if( w->second->GetType() & WIDGET_TYPE_SPECIAL) {
            w->second->SetupChars();
        }

        w->second->Start();
    }
}

      

And here's SetupChars ():

void WidgetGif::SetupChars() {
    Error("SetupChars <%s> <%s>", name_.c_str(), widget_base_.c_str());
    Error("Size of widgets: %d", visitor_->Widgets().size());
    std::map<std::string, Widget *> widgets = visitor_->Widgets();
    for(std::map<std::string, Widget *>::iterator ii=visitor_->Widgets().begin();
        ii != visitor_->Widgets().end(); ii++) {
        Error("<%s> Widget base %s == %s", ii->first.c_str(), ii->second->GetWidgetBase().c_str(), widget_base_.c_str());
        if(ii->second->GetWidgetBase() == widget_base_ &&
            ((WidgetGif *)ii->second)->HasChars()) {
            Error("Using chars from %s", ii->first.c_str());
            for(unsigned int i = 0; i < rows_ * cols_; i++ ) {
                if(i >= visitor_->GetLCDText()->CHARS) {
                    Error("1) GIF too large: %s, %d", name_.c_str(), visitor_->GetLCDText()->CHARS);
                    if(update_) delete update_;
                    update_ = new Property(visitor_, section_, "", new Json::Value("-1"));
                    return;
                }
                    ch_[i] = ((WidgetGif *)widgets[ii->first])->GetChars()[i];
            }
            hasChars_ = true;
            return;
        }
    }
// It goes on, but I snipped it here.
}

      

And here's what happens:

StartLayout: display_qt
Starting widget widget_gif_american_flag:layout_american_flag:0
SetupChars <widget_gif_american_flag:layout_american_flag:0> <layout_american_flag>
Size of widgets: 5
<widget_gif_american_flag:layout_american_flag:1> Widget base layout_american_flag == layout_american_flag
<widget_gif_american_flag:layout_american_flag:4> Widget base layout_american_flag == layout_american_flag
<(n
(n
  S> Widget base S == layout_american_flag
^C

      

Last edit: I figured it out. I just needed a copy of the original map for the new iterator.

+2


source to share


2 answers


There may be many reasons for this. First, it could be that GetType

either SetupChars

or Start

doing something that causes your map to change, which will invalidate the current iterator (note that using operator[]

on a map, even to read a value, is technically a mutational operation and can crash enabled when debugging an iterator!). Also, your card may be corrupted in memory by some code that was executed before, for example. due to a buffer overflow overwriting part of the map tree.



+5


source


You shouldn't change widgets_

in any of your methods GetType

, SetupChars

or Start

. This is most likely your problem.



If you must change widgets_

, you will need to restart the iterator whenever such a change is made. To avoid duplicating your changes, you can use a simple out-of-loop marker dictionary or a marker element of the Widget class.

0


source







All Articles