Original Post
I have recently been doing some coding and come across a very, very wierd bug which is impossible for me to duplicate. For the sheer sake of making things easier to diagnose for everyone, I wrote up this pseudo code: Essentially, you have a base class and a major class which contains a vector of base classes. Essentially, this is what I am doing in my code. The output of this is: The issue is, however, (and for some reason, its not in this code when you compile it...) that within my equivalent "addClass()" function, everytime I push I get size "1" or size "2", as it should be. However, outside the class function, in my main method, I am getting size "1539705258" and "3079410516"...every single time. But once I get back into the class method, I get 1 and 2 again, despite the fact I call size() almost back to back. I litterally call it one way (BaseClasses.size()), return to my main function, and call it again (as mc.BaseClasses.size()) and get something different. Does anyone have any idea what is going on? I have tried to isolate what is going on, calling things both in and outside the function. My actual code always crashses on this line: The line is basically the mc.BaseClasses[1].value equivalent line. Anyone have any ideas, or how to get more info on this? I have tried to gather as much as I could, but am at a complete loss. All the data in the base class loads correctly, and everything seems to be fine within the class method...but once I try to call it outside the class method, the program crashes (memory issue, I would assume). The output from my program I am getting is: It crashses when trying to access the name of the second script Thanks for taking the time to read this. -visage [Edited by - visage on November 28, 2004 6:05:35 PM]
#include <iostream>
#include <vector>
using namespace std;
class BaseClass{
public:
int value;
BaseClass(){ value = 0; }
};
class MajorClass{
public:
int numBaseClasses;
std::vector<BaseClass> BaseClasses;
MajorClass(){ numBaseClasses = 0; BaseClasses.clear(); }
~MajorClass(){ BaseClasses.clear(); }
int addClass();
};
int MajorClass::addClass(void){
cout << "Size of Major Class at top of add: " << BaseClasses.size() << endl;
BaseClass temp;
temp.value = ++numBaseClasses;
BaseClasses.push_back(temp);
cout << "Size of Major Class after adding " << numBaseClasses << " classes: " << BaseClasses.size() << endl;
return 1;
}
MajorClass mc;
int changeValue(BaseClass *bc, int value){
bc->value = value;
return 1;
}
int main(){
mc.addClass();
cout << "Size of Major Class after returning from add: " << mc.BaseClasses.size() << endl;
mc.addClass();
cout << "Size of Major Class after returning from add: " << mc.BaseClasses.size() << endl;
cout << mc.BaseClasses[0].value << endl;
cout << mc.BaseClasses[1].value << endl;
changeValue(&mc.BaseClasses[0], 5);
cout << mc.BaseClasses[0].value << endl;
system("PAUSE");
return 1;
}
Size of Major Class at top of add: 0
Size of Major Class after adding 1 classes: 1
Size of Major Class after returning from add: 1
Size of Major Class at top of add: 1
Size of Major Class after adding 2 classes: 2
Size of Major Class after returning from add: 2
1
2
5
cout << "Script Name 2: " << vm.scripts[1].szName << endl;
Size of Scripts before anything: 0
Size of scripts at top of add: 0
Size of scripts after push back: 1
Script name (in class method): thread1.vse
Size of Scripts after add (in main method): 1539705258
Size of scripts at top of add: 1
Size of scripts after push back: 2
Script name (in class method): thread2.vse
Size of Scripts after add (in main method): 3079410516
Script Name 1 (in main method): thread1.vse
Script Name 2 (in main method):