diff --git a/src/Pstream/mpi/UPstream.C b/src/Pstream/mpi/UPstream.C index 9eb2f37073618f32a31f314f6b427695bccdfbea..60636284a6a558a5a5146ba4bbdecc30d6d1d664 100644 --- a/src/Pstream/mpi/UPstream.C +++ b/src/Pstream/mpi/UPstream.C @@ -976,7 +976,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) {