Skip to content

Fix memory leaks and uninitialized memory access - #5108

Merged
akohlmey merged 14 commits into
lammps:developfrom
akohlmey:memcheck-fixes
Jul 26, 2026
Merged

Fix memory leaks and uninitialized memory access#5108
akohlmey merged 14 commits into
lammps:developfrom
akohlmey:memcheck-fixes

Conversation

@akohlmey

Copy link
Copy Markdown
Member

Summary

This pull request addresses issues found from running ctest -T memcheck.

Related Issue(s)

N/A

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

Claude Code Opus 5 assisted in analyzing the output from the test run and suggested individual fixes.

Backward Compatibility

Yes.

Implementation Notes

A lot of false positives can be avoided by using an improved valgrind command line.
This is included in the CMake configuration and described in tools/valgrind/README.

Post Submission Checklist

  • The feature or features in this pull request is complete
  • Licensing information is complete
  • Corresponding author information is complete
  • The source code follows the LAMMPS formatting guidelines
    system
  • The feature has been verified to work with the conventional build system
  • The feature has been verified to work with the CMake based build system
  • A package specific README file has been included or updated

akohlmey added 14 commits July 26, 2026 07:22
FixNVESpin::init() runs once per run command, so every run after the first
orphaned the previous rsec allocation.
The second pass assigns fp only for those local densities that apply to the
type of the central atom, but the force loop reads all of them, so the array
must be zeroed first.  The non-threaded pair style does this already.
A second pair_coeff command orphaned the entire set of tabulated arrays.
…g them

A second pair_style command leaked the previous generator: randomT in pair
style edpd, random in pair style sdpd/taitwater/isothermal.
The constructors declared a local variable of the same name, so the member
stayed uninitialized until the first compute, while memory_usage() reads it
as early as the first thermo output.
…the grid

ngrid is set by allocate(), which ran after the statistics were printed, so
the reported number of 3d grid values was undefined on the first run.  Both
PPPM and PPPMDipole have these two blocks in the opposite order.
Atoms without an entry in the Ellipsoids section kept an undefined radius,
which was then written to restart and dump files.
write_restart_settings() stores all 100 entries, not only those set with the
ecp keyword, so it wrote undefined data to the restart file.
Clearing input->arg orphaned the argument list that Input frees on delete.
Memory that is still reachable when the process exits is not a leak.  Those
reports bury the actual defects under thousands of one-time initializations
in OpenSSL, libcurl, CPython and GoogleTest and under everything that is
still in use when a command like quit terminates the process on purpose.

MEMORYCHECK_COMMAND_OPTIONS is a cache variable, so existing build folders
keep their current setting and have to be updated explicitly with
  cmake -D MEMORYCHECK_COMMAND_OPTIONS="..." <builddir>
@akohlmey akohlmey self-assigned this Jul 26, 2026
@akohlmey akohlmey moved this to In Progress in LAMMPS Pull Requests Jul 26, 2026
@akohlmey akohlmey added this to the Stable Release Summer 2026 milestone Jul 26, 2026
@akohlmey
akohlmey marked this pull request as ready for review July 26, 2026 15:36
@akohlmey akohlmey moved this from In Progress to Ready for Review or Merge in LAMMPS Pull Requests Jul 26, 2026
@akohlmey
akohlmey merged commit 2aea093 into lammps:develop Jul 26, 2026
9 checks passed
@github-project-automation github-project-automation Bot moved this from Ready for Review or Merge to Done in LAMMPS Pull Requests Jul 26, 2026
@akohlmey
akohlmey deleted the memcheck-fixes branch July 26, 2026 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

2 participants