-2

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.

chromedude
  • 4,246
  • 16
  • 65
  • 96
  • 1
    You really need to show the code that you have specifically removed from the example. What does "change curLine" do? What action does "do stuff" perform? Since `curLine` is a pointer, I guarantee that you're doing something funky with it, and it's nothing to do with using a `deque`. – paddy Feb 12 '14 at 02:28
  • The expert-designed `std::deque` does not have a memory leak. Stop blaming the tools, and start considering that you might be at fault. The memory leak is in _your_ code. Now, post a testcase. :) – Lightness Races in Orbit Feb 12 '14 at 02:29
  • You have ``deque lineBuffer(context);`` but you push ``lineBuffer.push_back(curLine);`` curline is string* – Krypton Feb 12 '14 at 02:32
  • Oh boy sorry @Krypton and @paddy - I changed a `string` to a `string*` when I removed the code (which was a pretty bad idea now that I think of it) so that should make things totally different. Just posted full code. I hope that makes more sense... – chromedude Feb 12 '14 at 02:37
  • @LightnessRacesinOrbit haha yeah I wasn't thinking it was an issue with deque - I'm sure it's my code, because this is literally the first I've really worked with C of any variety. – chromedude Feb 12 '14 at 02:38
  • What is the value of "context" at the beginning of this code? – PaulMcKenzie Feb 12 '14 at 02:40
  • @PaulMcKenzie context is dynamic, but it will be between 1 and 10. – chromedude Feb 12 '14 at 02:42
  • @chromedude - What is the *actual* value of context when that code is run? It could be a billion as far as I know. That means you take the debugger (or issue an output statement) and observe the value. Also what does the function doesLineMatch() do? That is the other wildcard we don't know about. – PaulMcKenzie Feb 12 '14 at 02:45
  • @PaulMcKenzie it is a value that the user defines when they run the program (as an argument). One of the ones it was failing with was `context = 3`. – chromedude Feb 12 '14 at 02:49
  • 1
    @chromedude - You also didn't tell us what "input" is, whether it points to actual valid memory, etc. (in addition to not posting the doesLineMatch() function). There is nothing wrong with pop_front() and push_back() if the strings are valid. So my guess is that somewhere you're corrupting memory and it is affecting the deque operations in some way, and possibly it is occurring in the code you're not showing us. – PaulMcKenzie Feb 12 '14 at 02:57
  • @PaulMcKenzie yeah... probably I am. I didn't think StackOverflow would really want me posting all my code here. I just put it in a PasteBin if you want to take a look at it: http://pastebin.com/Jp1RgwqV. If not don't worry about it, but I'd definitely really appreciate it. – chromedude Feb 12 '14 at 03:06
  • My guess is that I'm doing something really stupid with memory allocation because this is literally the first time I've written anything in C that really used pointers much and I really have barely a clue what I am doing. – chromedude Feb 12 '14 at 03:07

1 Answers1

1

From your pastebin link,

http://pastebin.com/Jp1RgwqV

please look at your parseInt() function. You're returning the address of a local variable, and that is a no-no.

int * parseInt(char *contextArg) {
  int resultingNumber = 0;
  //...
  int *endResult;
  endResult = &resultingNumber;
  return endResult;
}

See here:

Returning local data from functions in C and C++ via pointer

I would suggest you get more familiar with pointers, and the do's and dont's of pointer usage.

Community
  • 1
  • 1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • I also suggest dropping the usage of strlen() or C-string function usage anywhere in your program, as well as "new char[whatever]". There is no need for this in your program. Practically any string manipulation in the program can be done with std::string. – PaulMcKenzie Feb 12 '14 at 03:21