E
Ed Platt
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
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