]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix a bogus warning about a null pointer in a lambda. 13289/head
authorDavid Wells <drwells@email.unc.edu>
Mon, 24 Jan 2022 21:08:41 +0000 (16:08 -0500)
committerDavid Wells <drwells@email.unc.edu>
Mon, 24 Jan 2022 21:09:44 +0000 (16:09 -0500)
I get the following warning:

/home/drwells/Documents/Code/CPP/dealii-dev/source/base/mpi.cc: In static member function 'static constexpr void dealii::Utilities::MPI::create_mpi_data_type_n_bytes(std::size_t)::<lambda(ompi_datatype_t**)>::_FUN(ompi_datatype_t**)':
/home/drwells/Documents/Code/CPP/dealii-dev/source/base/mpi.cc:384:15: warning: 'this' pointer is null [-Wnonnull]
  384 |               }};
      |               ^
/home/drwells/Documents/Code/CPP/dealii-dev/source/base/mpi.cc:376:15: note: in a call to non-static member function 'dealii::Utilities::MPI::create_mpi_data_type_n_bytes(std::size_t)::<lambda(ompi_datatype_t**)>'
  376 |               [](MPI_Datatype *p) {
      |               ^

This isn't a problem since deleters don't store any kind of state but we can
work around it by splitting the constructor call.

source/base/mpi.cc

index 15f101bad7fda08dbdcb73296eb6677e60e49618..ed78d4f6e227d4fddf853fe04fd699f3a14fc173 100644 (file)
@@ -369,19 +369,22 @@ namespace Utilities
       // object, and as second argument a pointer-to-function, for which
       // we here use a lambda function without captures that acts as the
       // 'deleter' object: it calls `MPI_Type_free` and then deletes the
-      // pointer.
-      return {// The copy of the object:
-              new MPI_Datatype(result),
-              // The deleter:
-              [](MPI_Datatype *p) {
-                if (p != nullptr)
-                  {
-                    const int ierr = MPI_Type_free(p);
-                    AssertThrowMPI(ierr);
-
-                    delete p;
-                  }
-              }};
+      // pointer. To avoid a compiler warning about a null this pointer
+      // in the lambda (which don't make sense: the lambda doesn't store
+      // anything), we create the deleter first.
+      auto deleter = [](MPI_Datatype *p) {
+        if (p != nullptr)
+          {
+            const int ierr = MPI_Type_free(p);
+            (void)ierr;
+            AssertNothrow(ierr == MPI_SUCCESS, ExcMPI(ierr));
+
+            delete p;
+          }
+      };
+
+      return std::unique_ptr<MPI_Datatype, void (*)(MPI_Datatype *)>(
+        new MPI_Datatype(result), deleter);
     }
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.