Increase use of SafeFilePtr class#4917
Merged
Merged
Conversation
akohlmey
marked this pull request as ready for review
March 16, 2026 03:04
akohlmey
requested review from
athomps,
jrgissing,
sjplimp and
stanmoore1
as code owners
March 16, 2026 03:04
Closed
5 tasks
sjplimp
approved these changes
Mar 18, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR expands adoption of the SafeFilePtr RAII wrapper across LAMMPS to simplify FILE* lifetime management and reduce file descriptor leaks, and extends the wrapper to support pclose() for streams opened via popen() (e.g., on-the-fly compression).
Changes:
- Extend
SafeFilePtrwith apclose()option and move destructor/assignment logic into a newsafe_pointers.cpp. - Replace many
FILE*members/locals withSafeFilePtrand remove corresponding manualfclose()/pclose()calls. - Add developer documentation and Doxygen input integration for
SafeFilePtr.
Reviewed changes
Copilot reviewed 77 out of 77 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/write_restart.h | Replace FILE* member with SafeFilePtr. |
| src/write_restart.cpp | Use RAII close (fp=nullptr) and add earlier I/O error reduction. |
| src/write_data.h | Replace FILE* member with SafeFilePtr. |
| src/write_data.cpp | Use SafeFilePtr and mark compressed output for pclose(). |
| src/write_coeff.cpp | Use SafeFilePtr for temp/output coeff files. |
| src/variable.h | Replace FILE* member with SafeFilePtr for VarReader. |
| src/variable.cpp | Use SafeFilePtr for several temporary/utility file opens. |
| src/universe.cpp | Use SafeFilePtr in reorder file reading. |
| src/safe_pointers.h | Extend/document SafeFilePtr (including pclose() support). |
| src/safe_pointers.cpp | New: implementation of SafeFilePtr destructor/assignment with pclose() support. |
| src/reader.h | Replace FILE* with SafeFilePtr; remove explicit compressed member. |
| src/reader.cpp | Use SafeFilePtr for compressed/uncompressed open/close. |
| src/read_restart.h | Replace FILE* member with SafeFilePtr. |
| src/read_restart.cpp | Remove manual fclose() in favor of SafeFilePtr reset. |
| src/read_data.h | Replace FILE* member with SafeFilePtr; drop compressed flag. |
| src/read_data.cpp | Use SafeFilePtr and mark compressed reads for pclose(). |
| src/procmap.cpp | Use SafeFilePtr and improve custom-grid error messages. |
| src/platform.cpp | Use SafeFilePtr in file readability/writability helpers. |
| src/pair.cpp | Use SafeFilePtr for pair-table writing. |
| src/molecule.h | Replace FILE* member with SafeFilePtr. |
| src/molecule.cpp | Remove manual fclose() calls; minor formatting and rename print() parameter. |
| src/min_hftn.h | Replace progress FILE* with SafeFilePtr; remove explicit close helper decl. |
| src/min_hftn.cpp | Remove explicit fclose() management for progress file. |
| src/label_map.cpp | Use SafeFilePtr for label map writing. |
| src/fix_print.h | Replace FILE* member with SafeFilePtr. |
| src/fix_print.cpp | Remove manual fclose() in destructor; rely on RAII. |
| src/fix_ave_time.h | Replace FILE* member with SafeFilePtr. |
| src/fix_ave_time.cpp | Rely on RAII close; keep YAML trailer write logic. |
| src/fix_ave_histo.h | Replace FILE* member with SafeFilePtr. |
| src/fix_ave_histo.cpp | Remove manual fclose() in destructor. |
| src/fix_ave_correlate.h | Replace FILE* member with SafeFilePtr. |
| src/fix_ave_correlate.cpp | Remove manual fclose() in destructor. |
| src/fix_ave_chunk.h | Replace FILE* member with SafeFilePtr. |
| src/fix_ave_chunk.cpp | Remove manual fclose() in destructor. |
| src/dump.h | Replace FILE* member with SafeFilePtr. |
| src/dump.cpp | Remove manual close logic; use RAII and fp=nullptr on multifile writes. |
| src/citeme.h | Replace FILE* member with SafeFilePtr. |
| src/citeme.cpp | Remove manual fclose() in destructor. |
| src/balance.h | Replace FILE* member with SafeFilePtr. |
| src/balance.cpp | Remove manual fclose() in destructor. |
| src/RIGID/fix_rigid.cpp | Use SafeFilePtr for rigid restart sidecar file write. |
| src/PYTHON/python_impl.cpp | Use SafeFilePtr for Python file execution. |
| src/PHONON/fix_phonon.cpp | Use SafeFilePtr for map/binary output files. |
| src/ORIENT/fix_orient_fcc.cpp | Use SafeFilePtr for reading orientation data files. |
| src/ORIENT/fix_orient_eco.cpp | Use SafeFilePtr for reading orientation data file. |
| src/ORIENT/fix_orient_bcc.cpp | Use SafeFilePtr for reading orientation data files. |
| src/ML-SNAP/pair_snap.cpp | Use SafeFilePtr for SNAP coeff/param reads. |
| src/ML-RANN/pair_rann.cpp | Use SafeFilePtr for RANN potential file reads. |
| src/ML-POD/pair_pod.cpp | Use SafeFilePtr for binary debug outputs. |
| src/ML-POD/fitpod_command.cpp | Use SafeFilePtr for POD training IO and binary outputs. |
| src/ML-POD/eapod.cpp | Use SafeFilePtr for POD model file reads. |
| src/ML-IAP/mliap_model_nn.cpp | Use SafeFilePtr for NN coeff reads. |
| src/ML-IAP/mliap_descriptor_snap.cpp | Use SafeFilePtr for SNAP param reads. |
| src/MBX/fix_mbx.cpp | Use SafeFilePtr for JSON existence check. |
| src/MANYBODY/pair_eim.cpp | Use SafeFilePtr for EIM potential read/parse. |
| src/MACHDYN/fix_smd_wall_surface.cpp | Use SafeFilePtr for triangle mesh file reads. |
| src/KOKKOS/pair_pod_kokkos.cpp | Use SafeFilePtr for POD Kokkos binary outputs. |
| src/KOKKOS/pair_exp6_rx_kokkos.cpp | Use SafeFilePtr for EXP6-RX file reads. |
| src/INTEL/pair_snap_intel.cpp | Use SafeFilePtr for SNAP Intel coeff/param reads. |
| src/GRAPHICS/fix_graphics_labels.cpp | Use SafeFilePtr for image file reads. |
| src/GRAPHICS/fix_graphics_isosurface.cpp | Use SafeFilePtr for isosurface output file writes (including compressed). |
| src/GRAPHICS/dump_image.cpp | Use RAII close via fp=nullptr for multifile outputs. |
| src/EXTRA-FIX/fix_ttm_mod.cpp | Use SafeFilePtr for electron temperature output. |
| src/EXTRA-FIX/fix_ttm_grid.cpp | Use SafeFilePtr for grid file reads. |
| src/EXTRA-FIX/fix_ttm.cpp | Use SafeFilePtr for electron temperature output. |
| src/EXTRA-DUMP/dump_xtc.cpp | Remove explicit fp=nullptr handling (XTC uses separate handle). |
| src/EXTRA-COMMAND/geturl.cpp | Use SafeFilePtr for output file in curl download. |
| src/DPD-REACT/pair_multi_lucy.cpp | Use SafeFilePtr for table file reads. |
| src/DPD-REACT/pair_exp6_rx.cpp | Use SafeFilePtr for EXP6-RX file reads. |
| src/DPD-REACT/fix_rx.cpp | Use SafeFilePtr for RX kinetics/parameter file reads. |
| src/DPD-REACT/fix_eos_table_rx.cpp | Use SafeFilePtr for EOS table reads. |
| src/BOCS/fix_bocs.cpp | Use SafeFilePtr for reading BOCS table file. |
| src/ADIOS/reader_adios.cpp | Use SafeFilePtr for adios2_config.xml existence/create. |
| src/ADIOS/dump_custom_adios.cpp | Use SafeFilePtr for adios2_config.xml existence/create. |
| src/ADIOS/dump_atom_adios.cpp | Use SafeFilePtr for adios2_config.xml existence/create. |
| doc/src/Developer_utils.rst | Add Safe pointer documentation section and navigation tweaks. |
| doc/doxygen/Doxyfile.in | Include safe_pointers.h in Doxygen input list. |
You can also share your feedback on Copilot code review. Take the survey.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The SafeFilePtr class will automatically close files when their file pointer goes out of scope.
This helps to simplify code. This pull request expands the usage and adds to the documentation.
Author(s)
Axel Kohlmeyer, Temple U
Licensing
By submitting this pull request, I agree, that my contribution will be included in LAMMPS and redistributed under either the GNU General Public License version 2 (GPL v2) or the GNU Lesser General Public License version 2.1 (LGPL v2.1).
Artificial Intelligence (AI) Tools Usage
A first draft of some of the additional uses was generated by Claude Opus 4.6.
The more complex changes were done manually.
Backward Compatibility
No known issues.
Implementation Notes
This extends the existing class to include a flag if a
FILE *needs to be closed withpclose()instead offclose()to avoid file descriptor leaks.Post Submission Checklist