odd behavior in STL

Discussion in 'C++' started by Ed Platt, Jul 28, 2003.

  1. Ed Platt

    Ed Platt Guest

    I've been working on a project that uses the STL classes, and twice now
    I've run into some odd behavior I can't find the cause of. Basically,
    it seems as if a vector is changing its contents by itself. I've tested
    the code (C++ in a tcl wrapper) with gcc 2.96 on redhat 7.3 and gcc
    2.95.3 on slackware 8.0 and have similar errors on both.

    Here is some of the relevant code:

    void Graph::update() {
    printf("Graph::update()\n");
    for (vector<Component*>::iterator iter = myComponents.begin();
    iter < myComponents.end();
    iter++) {
    printf("updating %s(%d)\n", (*iter)->className(), (*iter)->myID());
    (*iter)->update();
    }
    ...
    printf("updated successfully\n");
    }

    And here is the output:

    graph loaded
    Graph::update()
    updating Component(3)
    updating Component(4)
    updating inductor(112)
    updating resistor(116)
    updating voltage(120)
    updating ground(126)
    updating junction(127)
    updating junction(128)
    updating junction(129)
    updated successfully
    updating node(49)
    updating capacitor(14)
    updating node(15)
    updating node(16)
    updating node(21)
    updating node(22)
    updating node(35)
    updating node(36)
    updating node(43)
    updating node(44)
    updating node(51)
    Segmentation fault

    There are a few really strange things about this.

    - myComponents is only changed in the function that prints "graph
    loaded", if I comment out the part that changes it then the program
    doesn't crash.

    - update() is usually called as part of a main loop, but the first
    output in the output is from a call I added at the end of the function
    that changes myComponents to make sure that the right values are getting
    put in there (which they are).

    - The first call to update() finishes, and another starts, but for some
    reason the second one doesn't print "Graph::update()".

    - myComponents is a vector<Component*> and although all of the classes
    in the output from the first time update() is called are subclasses of
    Component, the "node" class is not, but somehow it is in myComponents
    the second time update() is called.

    The first time I had a similar problem, I ended up using a different
    approach, and it just went away. I've looked over every possible thing
    I could think of causing this, and I can't find anything. Any ideas or
    similar experiences? (Reply to group, email is not valid).

    -Ed
     
    Ed Platt, Jul 28, 2003
    #1
    1. Advertising

  2. Ed Platt

    Ed Platt Guest

    Thanks. I've made that and all similar changes in the code. I'm still
    having the exact same problem though, so let me know if anyone knows of
    a possible cause.

    -Ed

    Xenos wrote:
    > You should not use "iter < myComponents.end()". It should read "iter !=
    > myComponents.end()"
    >
    >
    > "Ed Platt" <> wrote in message
    > news:...
    >
    >>I've been working on a project that uses the STL classes, and twice now
    >>I've run into some odd behavior I can't find the cause of. Basically,
    >>it seems as if a vector is changing its contents by itself. I've tested
    >>the code (C++ in a tcl wrapper) with gcc 2.96 on redhat 7.3 and gcc
    >>2.95.3 on slackware 8.0 and have similar errors on both.
    >>
    >>Here is some of the relevant code:
    >>
    >>void Graph::update() {
    >> printf("Graph::update()\n");
    >> for (vector<Component*>::iterator iter = myComponents.begin();
    >> iter < myComponents.end();
    >> iter++) {
    >> printf("updating %s(%d)\n", (*iter)->className(), (*iter)->myID());
    >> (*iter)->update();
    >> }
    >> ...
    >> printf("updated successfully\n");
    >>}
    >>
    >>And here is the output:
    >>
    >>graph loaded
    >>Graph::update()
    >>updating Component(3)
    >>updating Component(4)
    >>updating inductor(112)
    >>updating resistor(116)
    >>updating voltage(120)
    >>updating ground(126)
    >>updating junction(127)
    >>updating junction(128)
    >>updating junction(129)
    >>updated successfully
    >>updating node(49)
    >>updating capacitor(14)
    >>updating node(15)
    >>updating node(16)
    >>updating node(21)
    >>updating node(22)
    >>updating node(35)
    >>updating node(36)
    >>updating node(43)
    >>updating node(44)
    >>updating node(51)
    >>Segmentation fault
    >>
    >>There are a few really strange things about this.
    >>
    >>- myComponents is only changed in the function that prints "graph
    >>loaded", if I comment out the part that changes it then the program
    >>doesn't crash.
    >>
    >>- update() is usually called as part of a main loop, but the first
    >>output in the output is from a call I added at the end of the function
    >>that changes myComponents to make sure that the right values are getting
    >>put in there (which they are).
    >>
    >>- The first call to update() finishes, and another starts, but for some
    >>reason the second one doesn't print "Graph::update()".
    >>
    >>- myComponents is a vector<Component*> and although all of the classes
    >>in the output from the first time update() is called are subclasses of
    >>Component, the "node" class is not, but somehow it is in myComponents
    >>the second time update() is called.
    >>
    >>The first time I had a similar problem, I ended up using a different
    >>approach, and it just went away. I've looked over every possible thing
    >>I could think of causing this, and I can't find anything. Any ideas or
    >>similar experiences? (Reply to group, email is not valid).
    >>
    >>-Ed
    >>

    >
    >
    >
     
    Ed Platt, Jul 28, 2003
    #2
    1. Advertising

  3. "Ed Platt" <> wrote in message
    news:...
    > Thanks. I've made that and all similar changes in the code. I'm still
    > having the exact same problem though, so let me know if anyone knows of
    > a possible cause.
    >


    I think you are going to have to post more code.

    john
     
    John Harrison, Jul 29, 2003
    #3
  4. Ed Platt

    Ed Platt Guest

    Thanks for the help everyone, I just figured it out. It's rediculously
    simple now that I see it, but I suppose things like this tend to be like
    that. It turns out that one of the Component pointers being updated
    changed myComponents and invalidated the iterator. Here's an
    explanation of all the odd behavior:

    > - myComponents is only changed in the function that prints "graph
    > loaded", if I comment out the part that changes it then the program
    > doesn't crash.


    This is actually happening inside Graph::update().

    > - update() is usually called as part of a main loop, but the first
    > output in the output is from a call I added at the end of the function
    > that changes myComponents to make sure that the right values are getting
    > put in there (which they are).


    Because the function that changes myComponents is called in
    Graph::update(), the second call to Graph::update() is actually before
    the first one has finished.

    > - The first call to update() finishes, and another starts, but for some
    > reason the second one doesn't print "Graph::update()".


    The second call is actually finishing. It has a valid iterator because
    it is called after myComponents has changed. Once it returns, the
    iterator in the original Graph::update() is invalid and causes the crash.

    > - myComponents is a vector<Component*> and although all of the classes
    > in the output from the first time update() is called are subclasses of
    > Component, the "node" class is not, but somehow it is in myComponents
    > the second time update() is called.


    Invalid iterator, I wish STL handled things like this better.

    -Ed
     
    Ed Platt, Jul 29, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elliot M. Rodriguez

    PLEASE HELP = odd TextChanged behavior

    Elliot M. Rodriguez, Oct 21, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    323
    Elliot M. Rodriguez
    Oct 22, 2003
  2. Guest

    Step-thru code - odd behavior

    Guest, May 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    388
    Guest
    Jun 1, 2004
  3. =?Utf-8?B?Q2hyaXM=?=
    Replies:
    1
    Views:
    331
    Karl Seguin
    Jan 7, 2005
  4. news.microsoft.com

    EXTREMELY odd cookie behavior

    news.microsoft.com, Mar 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    312
    Joerg Jooss
    Mar 15, 2005
  5. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,114
    Richard Heathfield
    Feb 18, 2007
Loading...

Share This Page