Segmentation fault (Core dumped) error

Hi, I have been working on a profiler project for class for a few weeks now. A few days ago I ran into an error when running the program saying - Segmentation fault (Core dumped). I have asked around and still not able to figure it out.

Here is the method I believe is causing the fault.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
// Adds in the includes and profile variables in a main file.

void ASTree::mainHeader(std::vector<std::string>& profileNames) {
 
  std::string header;
  std::string filename;
  ASTree *final = new ASTree();
  header = "#include \"profile.hpp\" \n";
  
  ASTree *inclCat = new ASTree(category);
  inclCat ->tag = "&lt;inclCat&gt;";
  inclCat ->closeTag = "&lt;/inclCat&gt;";
  ASTree *inclToken = new ASTree(token, header);
  inclCat -> child.push_back(inclToken);
  final -> child.push_back(inclCat);
  
  ASTree *variableDecl = new ASTree(category);
  variableDecl ->tag = "&lt;variableDecl&gt;";
  variableDecl ->closeTag = "&lt;/variableDecl&gt;";
  std::vector<std::string>::iterator it = profileNames.begin();

  while(it != profileNames.end()) {
    std::string profVariable = "profile " + *it;
    filename = *it;
    std::replace(filename.begin(), filename.end(), '_', '.');  
    profVariable = profVariable +"(\""+ filename +"\");";
    profVariable += '\n';
    ASTree *variableToken = new ASTree(token,profVariable);
    variableDecl ->child.push_back(variableToken);
    ++it;
  }

  child.push_front(variableDecl);
  child.push_front(final);
  
}


Sorry if it's kind of vague, I can attach more code if needed.

Thanks so much for reading!
You should be able to identify the line causing the trouble, and with it which bad pointer you're using. Here's a brief introduction: http://www.cplusplus.com/articles/iwTbqMoL/
¿is there a good reason why you can't have std::vector<ASTree> child? (objects instead of pointers)
I don't see anything wrong with this code. It's possible that the problem is actually elsewhere and it just doesn't show up until you hit this code. That's the nasty thing about heap corruption. The symptom is in New York but the cause is in Kansas.
Thanks Repeater!

I found the error to be somewhere in this section according to the debugger:
More specifically, with the while loop.

1
2
3
4
5
6
7
8
9

std::list<AST*>::iterator it = child.begin();
AST *newPtr = (*it)->getChild("block");
std::list<AST*>::iterator itr = newPtr->child.begin();

while ((*itr)->tag != "return") {
    ++itr;
}
Last edited on
1
2
std::list<AST*>::iterator it = child.begin();
AST *newPtr = (*it)->getChild("block");
You should check if it == child.end() before using it.
Topic archived. No new replies allowed.