From 23c55b0046ce9330b6f0b76d6cb8a3c805b22558 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 11 May 2020 16:23:04 -0400 Subject: [PATCH] fix "use of class after delete" bug --- src/variable.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/variable.cpp b/src/variable.cpp index 0fc53f8074..34258522c4 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -661,6 +661,8 @@ int Variable::next(int narg, char **arg) } else if (istyle == UNIVERSE || istyle == ULOOP) { + RanMars *random = nullptr; + uloop_again: // wait until lock file can be created and owned by proc 0 of this world @@ -674,7 +676,7 @@ int Variable::next(int narg, char **arg) int nextindex = -1; if (me == 0) { int seed = 12345 + universe->me + which[find(arg[0])]; - RanMars *random = new RanMars(lmp,seed); + if (!random) random = new RanMars(lmp,seed); int delay = (int) (1000000*random->uniform()); usleep(delay); while (1) { @@ -682,7 +684,6 @@ int Variable::next(int narg, char **arg) delay = (int) (1000000*random->uniform()); usleep(delay); } - delete random; // if the file cannot be found, we may have a race with some // other MPI rank that has called rename at the same time @@ -706,6 +707,9 @@ int Variable::next(int narg, char **arg) delay = (int) (1000000*random->uniform()); usleep(delay); } + delete random; + random = nullptr; + if (nextindex < 0) error->one(FLERR,"Unexpected error while incrementing uloop " "style variable. Please contact LAMMPS developers.");