From f8da51828a11eefe52f1ca8a2f09890d160aa897 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 19 Mar 2024 12:03:56 -0400 Subject: [PATCH] fix memory leaks in lammps_gather*concat() functions of the library interface --- src/library.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/library.cpp b/src/library.cpp index fcf0f6a631..1f76c783da 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -2857,10 +2857,6 @@ void lammps_gather_atoms_concat(void *handle, const char *name, int type, if ((count == 1) || imgunpack) vector = (int *) vptr; else array = (int **) vptr; - int *copy; - lmp->memory->create(copy,count*natoms,"lib/gather:copy"); - for (i = 0; i < count*natoms; i++) copy[i] = 0; - int nlocal = lmp->atom->nlocal; if (count == 1) { @@ -2872,7 +2868,10 @@ void lammps_gather_atoms_concat(void *handle, const char *name, int type, MPI_INT,lmp->world); } else if (imgunpack) { - lmp->memory->create(copy,count*nlocal,"lib/gather:copy"); + int *copy; + lmp->memory->create(copy,count*natoms,"lib/gather:copy"); + for (i = 0; i < count*natoms; i++) copy[i] = 0; + offset = 0; for (i = 0; i < nlocal; i++) { const int image = vector[i]; @@ -4328,10 +4327,6 @@ void lammps_gather_concat(void *handle, const char *name, int type, int count, if ((count == 1) || imgunpack) vector = (int *) vptr; else array = (int **) vptr; - int *copy; - lmp->memory->create(copy,count*natoms,"lib/gather:copy"); - for (i = 0; i < count*natoms; i++) copy[i] = 0; - int nlocal = lmp->atom->nlocal; if (count == 1) { @@ -4343,7 +4338,10 @@ void lammps_gather_concat(void *handle, const char *name, int type, int count, MPI_INT,lmp->world); } else if (imgunpack) { - lmp->memory->create(copy,count*nlocal,"lib/gather:copy"); + int *copy; + lmp->memory->create(copy,count*natoms,"lib/gather:copy"); + for (i = 0; i < count*natoms; i++) copy[i] = 0; + offset = 0; for (i = 0; i < nlocal; i++) { const int image = vector[i];