From: Matthias Maier <tamiko@43-1.org>
Date: Mon, 19 Apr 2021 20:43:20 +0000 (-0500)
Subject: free allocated MPI resources before we call MPI_Finalize
X-Git-Tag: v9.3.0-rc1~208^2~1
X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e1b8756608771ad5ed2cdd66ca91d40f3bcec5a0;p=dealii.git

free allocated MPI resources before we call MPI_Finalize
---

diff --git a/source/base/mpi.cc b/source/base/mpi.cc
index 2a71e5c988..f72b60715c 100644
--- a/source/base/mpi.cc
+++ b/source/base/mpi.cc
@@ -623,19 +623,10 @@ namespace Utilities
         }
 
       /*
-       * Create a custom MPI datatype handle describing the memory layout
-       * of the MinMaxAvg struct and a custom MPI op handle for our
-       * max_reduce function.
-       *
-       * This setup will leak some minor allocated memory (in the range of
-       * a few bytes) at the end of the program run because we never call
-       * MPI_Type_free(&type) and MPI_Op_free(&op) to explicitly free up
-       * memory. This is a cosmetic issue but some address sanitizers will
-       * complain about this (such as clang's -fsanitize=address).
+       * A custom MPI datatype handle describing the memory layout of the
+       * MinMaxAvg struct. Initialized on first pass control reaches the
+       * static variable. So hopefully not initialized too early.
        */
-
-      /* Initialized on first pass control reaches the static variable. So
-       * hopefully not initialized too early. */
       static MPI_Datatype type = []() {
         MPI_Datatype type;
 
@@ -654,11 +645,21 @@ namespace Utilities
         ierr = MPI_Type_commit(&type);
         AssertThrowMPI(ierr);
 
+        /* Ensure that we free the allocated datatype again at the end of
+         * the program run just before we call MPI_Finalize():*/
+        MPI_InitFinalize::signals.at_mpi_finalize.connect([type]() mutable {
+          int ierr = MPI_Type_free(&type);
+          AssertThrowMPI(ierr);
+        });
+
         return type;
       }();
 
-      /* Initialized on first pass control reaches the static variable. So
-       * hopefully not initialized too early. */
+      /*
+       * A custom MPI op handle for our max_reduce function.
+       * Initialized on first pass control reaches the static variable. So
+       * hopefully not initialized too early.
+       */
       static MPI_Op op = []() {
         MPI_Op op;
 
@@ -668,6 +669,13 @@ namespace Utilities
                         &op);
         AssertThrowMPI(ierr);
 
+        /* Ensure that we free the allocated op again at the end of the
+         * program run just before we call MPI_Finalize():*/
+        MPI_InitFinalize::signals.at_mpi_finalize.connect([op]() mutable {
+          int ierr = MPI_Op_free(&op);
+          AssertThrowMPI(ierr);
+        });
+
         return op;
       }();