Sorry. The code that does trigger the error is much, much longer. About ...3000 lines longer :D I was trying to recreate the error and simply cant...except it seems to continue to exist in my other code.
Ignore anything about threads. Its for my scripting language, and im trying to get several scripts running at once...hence why I call them thread1 and thread2. Has nothing to do with race conditions. A good thought though.
Below is the actual code:
/* WITHIN MY MAIN METHOD*/voodoomachine vm;cout << "Size of Scripts before anything: " << vm.scripts.size() << endl; r = vm.loadScript("thread1.vse"); cout << "Size of Scripts after add (in main method): " << vm.scripts.size() << endl; if(r < 0){ cout << "Could not load script \"thread1.vse\"." << endl; } r = vm.loadScript("thread2.vse"); cout << "Size of Scripts after add (in main method): " << vm.scripts.size() << endl; if(r < 0){ cout << "Could not load script \"thread2.vse\"." << endl; } cout << "Script Name 1: " << vm.scripts[0].szName << endl; cout << "Script Name 2: " << vm.scripts[1].szName << endl;
/* LOADSCRIPT method*/int voodoomachine::loadScript(std::string filename){ FILE *fp; voodooscript tempScript; std::string tempChar; int instrStreamSize; cout << "Size of scripts at top of add: " << scripts.size() << endl; fp = fopen(filename.c_str(), "rb"); if(fp == NULL){ return FILE_DOES_NOT_EXIST; } //the file is open tempScript.szName = filename; for(int i = 0; i < 5; i++){ tempChar += fgetc(fp); } tempChar += '\0'; if(strcmp(tempChar.c_str(), CURR_VER)) return INCORRECT_VERSION; tempChar.clear(); fread(&tempScript.header.iStackSize, sizeof(int), 1, fp); if(tempScript.header.iStackSize == 0) tempScript.header.iStackSize = 1024; tempScript.stack = (Op*)malloc(sizeof(Op)*tempScript.header.iStackSize); if(tempScript.stack == NULL) return -1; for(int i = 0; i < tempScript.header.iStackSize; i++){ tempScript.stack.iType = 0; tempScript.stack.iIntLiteral = 0; } fread(&tempScript.header.iGlobalDataSize, sizeof(int), 1, fp); fread(&tempScript.header.iIsMainFunction, sizeof(int), 1, fp); fread(&tempScript.header.iMainFunctionIndex, sizeof(int), 1, fp); fread(&tempScript.header.iPriority, sizeof(int), 1, fp); fread(&tempScript.header.iIsConstruct, sizeof(int), 1, fp); fread(&tempScript.header.iConstructIndex, sizeof(int), 1, fp); fread(&tempScript.header.iIsDeconstruct, sizeof(int), 1, fp); fread(&tempScript.header.iDeconstructIndex, sizeof(int), 1, fp); fread(&tempScript.iNumLines, sizeof(int), 1, fp); tempScript.loadOps(fp, tempScript.iNumLines); cout << "Size of scripts after loadops: " << scripts.size() << endl; int numFuncs; fread(&numFuncs, sizeof(int), 1, fp); for(int i = 0; i < numFuncs; i++){ tempChar.clear(); function temp; int nameSize; fread(&temp.iIndex, sizeof(int), 1, fp); fread(&nameSize, sizeof(int), 1, fp); for(int i = 0; i < nameSize; i++){ tempChar += fgetc(fp); } temp.szName = tempChar; fread(&temp.iEntryPoint, sizeof(int), 1, fp); fread(&temp.iParamCount, sizeof(int), 1, fp); fread(&temp.iLocalDataSize, sizeof(int), 1, fp); tempScript.funcTable.push_back(temp); } int numObjects; fread(&numObjects, sizeof(int), 1, fp); for(int i = 0; i < numObjects; i++){ tempChar.clear(); object temp; int nameSize; fread(&temp.iIndex, sizeof(int), 1, fp); fread(&nameSize, sizeof(int), 1, fp); for(int i = 0; i < nameSize; i++){ tempChar += fgetc(fp); } temp.szName = tempChar; fread(&temp.iSize, sizeof(int), 1, fp); fread(&temp.iStackIndex, sizeof(int), 1, fp); fread(&temp.iFuncIndex, sizeof(int), 1, fp); tempScript.objectTable.push_back(temp); } int numStrings; fread(&numStrings, sizeof(int), 1, fp); for(int i = 0; i < numStrings; i++){ tempChar.clear(); int nameSize; fread(&nameSize, sizeof(int), 1, fp); for(int i = 0; i < nameSize; i++){ tempChar += fgetc(fp); } tempScript.stringTable.push_back(tempChar); } int numLabels; fread(&numLabels, sizeof(int), 1, fp); for(int i = 0; i < numLabels; i++){ tempChar.clear(); label temp; int nameSize; fread(&temp.iIndex, sizeof(int), 1, fp); fread(&nameSize, sizeof(int), 1, fp); for(int i = 0; i < nameSize; i++){ tempChar += fgetc(fp); } temp.szName = tempChar; fread(&temp.iTargetIndex, sizeof(int), 1, fp); fread(&temp.iFuncIndex, sizeof(int), 1, fp); tempScript.labelTable.push_back(temp); } tempScript.iScriptNum = scriptNum++; if(fgetc(fp) != EOF) return LAST_MINUTE_READ_ERROR; fclose(fp); if(tempScript.header.iIsMainFunction == 1){ //the current line should be the iEntry point of the function located at //the iMainFunctionIndex //PushFrame(&tempScript, tempScript.funcTable[tempScript.header.iMainFunctionIndex].iLocalDataSize); tempScript.iCurLine = tempScript.funcTable[tempScript.header.iMainFunctionIndex].iEntryPoint; tempScript.iIsDone = 0; } else{ tempScript.iIsDone = 1; } //push it onto our scripts scripts.push_back(tempScript); cout << "Size of scripts after pushback: " << scripts.size() << endl; cout << "Script name (in class method): " << scripts[scriptNum-1].szName << endl; return 1;}
Thanks for looking!