I am building a small C++ program that needs to use a deque to manage some dynamic data. I built the script and works great when there are small amounts of data put in and taken out of the deque, but when a good amount of data is put in and out the program errors out with a Memory Fault. Here is the relevant code:
string curLine;
deque<string> lineBuffer(context);
int linesAfterContext = 0;
ifstream newFile;
istream *in;
if (input == NULL) {
in = &cin;
}
else {
newFile.open(input);
in = &newFile;
if (newFile.fail() || !newFile.is_open()) {
string error = "Not able to open that file. Please provide a valid file.";
throw error;
}
}
while(in->good()) {
getline(*in, curLine);
if (doesLineMatch(curLine)) {
if (linesAfterContext == 0) {
for (int i = 0; i < lineBuffer.size(); i++) {
string curLineInBuffer = lineBuffer.at(i);
if (!curLineInBuffer.empty()) {
cout << lineBuffer.at(i) << endl;
}
}
}
cout << curLine << endl;
linesAfterContext = context;
}
else {
if (linesAfterContext > 0) {
cout << curLine << endl;
linesAfterContext--;
}
if (lineBuffer.size() == context) {
lineBuffer.pop_front();
}
lineBuffer.push_back(curLine);
}
}
if (input != NULL) {
newFile.close();
}
The problem is clearly with how I am pushing and poping the deque because when I comment out those four lines the Memory Fault doesn't happen anymore. Any ideas why these lines would be leaking memory?
Edit:
Ok so I just posted the full code. I edited some variables in the original code without thinking about the effect they would have on how memory was managed. I am a total newbie with C++ (or C anything really as proven =P) so I'm sure it's an issue with my code not deque. Sorry about the confusion.