c++ - I am getting a resource leak : fin error when the code worked previously -
c++ - I am getting a resource leak : fin error when the code worked previously -
i've read few threads said should close file when error pops when utilize cppcheck. issue this:
2 weeks ran shell script called file within execute , worked fine. past 2 days, got segmentation fault in when running main code using cygwin. had posted earlier, , on analysing executable file (in cpp) using cppcheck, got line 31 resource leak : fin.
this particular block of code pasted:
void load_fasta_list(char * file_name, vector<string> &file_list){ file * fin; fin = fopen(file_name, "rt"); char temp_file[512]; char * temp_file2; while (!feof(fin)){ fgets(temp_file, 512, fin); if (!feof(fin)){ temp_file2 = strtok(temp_file, "\n"); file_list.push_back(temp_file2); } } cout<<file_list.size()<<" fasta files analyzed."<<endl;
}
the line 31 lastly bracket there. these codes available washington u using (and beginner), , getting error without having done set of codes.
any thought on how progress?
p.s. when tried fclose statement, cppcheck showed no error, when ran shell script again, got segmentation fault
the problem lecturer taught c instead of c++. used primitive c build open file must manually closed instead of automatic cleanup offered c++. forgot so, aptly demonstrating why using such constructs inherently unsafe. exception-unsafe, , there other unpleasant potential bugs lurking in here such off-by-one errors, , fun non-reentrant strtok, due utilize of c string handling.
you should rewrite (or create lecturer prepare it) utilize equivalent c++ constructs, automatically clean memory , file handles needed.
the code contains other offences too, output parameter, using namespace std;
, , such. whoever wrote unfit teach c++. need kick them gear.
c++ memory-leaks cygwin
Comments
Post a Comment