diff --git a/src/Pstream/mpi/UPstream.C b/src/Pstream/mpi/UPstream.C index 65db8482068ec17c1b038e00a03cdd482cc13baf..a698194dabbf03f2eba858c85671903fe6aa9ced 100644 --- a/src/Pstream/mpi/UPstream.C +++ b/src/Pstream/mpi/UPstream.C @@ -969,7 +969,17 @@ void Foam::UPstream::allocatePstreamCommunicator void Foam::UPstream::freePstreamCommunicator(const label communicator) { - if (communicator != UPstream::worldComm) + // 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) {