hzambran / hydroPSO

Model-Independent Particle Swarm Optimisation for Environmental Models
https://cran.r-project.org/package=hydroPSO
GNU General Public License v2.0
36 stars 18 forks source link

Close connections on unclean exit #7

Open jthurner opened 7 years ago

jthurner commented 7 years ago

Currently hydroPSO leaves dangling fileconnections and the parallel cluster (if used) open in case the function exits unorderly (errors, hitting escape in the console), which can lead to problems on subsequent runs. These commits use on.exit() to clean up connections.

The fix for the file connections looks a bit messy - but setting them to NULL after closing and checking for NULL before closing is required since if (isopen(fcon) close(fcon) throws an error if fcon has been closed previously (discussion). Patch only guards the files opened in the main loop if writedisk is TRUE, but that should be where the problem occurs in nearly all of the cases.

If you want to have this solved differently, let me know and I can revise.