diff --git a/src/Pstream/mpi/UPstream.C b/src/Pstream/mpi/UPstream.C index 0964e754ccb7b50c5e43c45c6d0c378bb413af14..a278bfb6be4641bbac293cc423b9d085ad217dae 100644 --- a/src/Pstream/mpi/UPstream.C +++ b/src/Pstream/mpi/UPstream.C @@ -1132,7 +1132,17 @@ void Foam::UPstream::allocatePstreamCommunicator void Foam::UPstream::freePstreamCommunicator(const label communicator) { - if (communicator != 0) + // Not touching the first communicator (WORLD) + // or anything out-of bounds. + // + // No UPstream communicator indices when MPI is initialized outside + // of OpenFOAM - thus needs a bounds check too! + + if + ( + communicator > 0 + && (communicator < PstreamGlobals::MPICommunicators_.size()) + ) { if (PstreamGlobals::MPICommunicators_[communicator] != MPI_COMM_NULL) {