Segfault in destructor with LAMMPS 29 Aug 2015 + fix/workaround

Hi all,

For some reason, the newest version of LAMMPS caused segfaults in its destructor for me. First I thought this might be something stupid on my part, so I cloned into a “vanilla” version from git.lammps.org, but that one seems to have it as well. If I just compile and run the melt example, everything works fine, except at the end of the run, a segfault occurs. GDB says this:
Program received signal SIGSEGV, Segmentation fault.

0x000000000098de57 in LAMMPS_NS::LAMMPS::~LAMMPS (this=0x1274d40, __in_chrg=) at …/lammps.cpp:549
549 int me = comm->me;

I think this is because at the top of the destructor, “destroy()” is called, which in turn destroys comm, and then in the next line in ~lammps(), comm is dereferenced in comm->me.

Moving the destruction to below where the screen and log are closed seems to prevent the problem, but I am not sure if this is the correct way to go. If it is, just apply the attached patch. If not, Steve will probably know what do do instead. :wink:

lammps.cpp.patch (505 Bytes)

Hi all,

For some reason, the newest version of LAMMPS caused segfaults in its
destructor for me.

​yes, in the past there were some hard to find intermittent bugs where
pointers were used after they were freed, or classes used after they were
deleted. the last patch resets some more of those pointers now explicitly
to NULL so we can detect this with a "clean" segmentation fault.
expect to see a few more of those as people start using newer versions and
features that are not currently covered by the automated continuous
integration tests.

First I thought this might be something stupid on my part, so I cloned
into a "vanilla" version from git.lammps.org, but that one seems to have
it as well. If I just compile and run the melt example, everything works
fine, except at the end of the run, a segfault occurs. GDB says this:
Program received signal SIGSEGV, Segmentation fault.

0x000000000098de57 in LAMMPS_NS::LAMMPS::~LAMMPS (this=0x1274d40,
__in_chrg=<optimized out>) at ../lammps.cpp:549
549 int me = comm->me;

I think this is because at the top of the destructor, "destroy()" is
called, which in turn destroys comm, and then in the next line in
~lammps(), comm is dereferenced in comm->me.

​correct, this should already be fixed in the git repo with the last
synchronization from half an hour ago.​

​axel.​