c++ - Can't figure out this memory bug (crash after program finishes) -
entire program: http://pastebin.com/x7gfsy5v
i made parser takes lines .txt file , places them in binary search tree based on value. id first string, values following strings separated /
example txt:
abcd/foo/bar// #id/value/value2 abce/foo2// #id/value
the first line call:
//parser sequencemap a; #has id , value member variables a.writeacronym(id); #abcd a.writevalue(value); #foo insertnode(a, root); #abcd/foo put tree a.writevalue(value2); #bar insertnode(a, root); #abcd/bar put tree
creating tree doesn't cause crash, , works should. it's when try accessing minimum value of tree when crashes. still shows correct minimum value though.
here's function gets minimum value, doubt root cause of problem.
but running in main() crashes program:
string printmin(node * x) { if (x == nullptr) return nullptr; //this isn't ever called, parse tree before calling fucntion if (x->left == nullptr) return x -> sqmap.getacronym(); //returns correct value (end of program) crash else printmin(x->left); } void printmain(){ cout << printmin(root) << endl; }
main():
a.parse("foo.txt"); //doesn't crash a.printmain(); //crashes when called //but doesn't crash if remove both a.writevalue(value) parser
here insert/parser:
void parse(string file) //uses delimiter split lines { string line, id, value, value2; ifstream myfile(file); if (myfile.is_open()) { while(getline(myfile, line)) { if (!line.empty()) { sequencemap a; istringstream is(line); getline(is, id, '/'); a.writeacronym(id); getline(is,value, '/'); a.writevalue(value); //program doesn't crash if remove insertnode(a, root); getline(is,value2,'/'); if (value2 != "") { a.writevalue(value2); //program doesn't crash if remove insertnode(a, root); } } } myfile.close(); } else cout << "could not open file."; } /*****************************************************/ void insertnode(sequencemap & sq, node * & x) { if (x == nullptr) x = new node(sq, nullptr, nullptr); else if (sq.getvalue() < x->sqmap.getvalue()) insertnode(sq, x->left); else if (sq.getvalue() > x->sqmap.getvalue()) insertnode(sq, x->right); else if (sq.getvalue() == x->sqmap.getvalue()) x->sqmap.merge(sq); }
i'm stuck right now, guess it's memory bug. works should, , correct minimum value. right after last line of code ends, crash. made destructor tree didn't solve it. can figure out?
printmin
doesn't return value when traverse left node, garbage return , not string
. if compile warnings way should warning that.
Comments
Post a Comment