I'm having a tricky debugging issue, perhaps due to my lack of understanding about how c++ manages memory. The code is too long to post, but the essential setup is as follows:
global_var = 0;
int main() {
for(i = 0; i < N; ++i) {
ClassA a;
new ClassB(a); // seems to be problem!
}
}
For some N
, global_var
gets corrupted (is no longer 0). There is nothing in the constructors of ClassA or ClassB that mess with global_var
.
Replacing new ClassB(a)
with ClassB b(a)
seems to solve the problem, although this doesn't allow me to do what I want (create a boost::ptr_vector
with the new ClassB(a)
instances).
Any ideas on what might be going wrong?
Update: I'm really doing something like:
global_var = 0;
int main() {
boost::ptr_vector<ClassB> myobjects;
for(i = 0; i < N; ++i) {
ClassA a;
myobjects.push_back(new ClassB(a)); // seems to be problem!
}
}
Both create problems. But why is this a problem? Should I be doing something else to put a bunch of objects into a queue? I'm using myobjects
it as the basis of a Command Pattern.
Update
`classB' looks like:
class ClassB {
public:
ClassB() {}
ClassB(ClassA a) : a_(a) {}
private:
ClassA a_;
}
ClassA is just a simple list initialization as well (in real life).
Problem?
Update I believe this may have something to do with the fact that global_var is actually a complex matrix type and there may be issues with the way it allocates memory.