I am writing to a file using a pointer variable that points to my File object, which includes the method used to write to my text file. When I try to delete the memory my program crashes... The error does not occur when I comment out the "delete file;" line. The rest of the code runs succesfully apart from the delete function. I want to avoid any possible memory leaks, please help.
Here's my code:
void change_registry_config(){
try{
file = new Files(file_path[1], WRITE_TO_FILE, IOS_OUT); //predefined class
set_data(); //function that overwrites the data in the vector
file->write_file(*data); //writes vector to file
}catch(file_ex ex){
SetWindowTextA(hEdit, ex.what());
}catch(exception e){
}
if(file != NULL){
delete file; //error occurs here, possible memory leak
}
}
Code in header file used to write to file:
void Files::write_file(std::vector<std::string>& vect){
if(!write_out->fail()){
for(unsigned i = 0; i < vect.size(); i++){
*write_out << vect.at(i) << std::endl;
}
write_out->close();
}else{
file_ex er; //custom error class extending std::exception
er.set_msg("Could not access file");
throw er;
}
}