title: Segmentation fault in MPI_Test() when using singleton/wrapper classes
body: <p>During my work writing a C++ wrapper for MPI I ran into a segmentation fault in <code>MPI_Test()</code>, the reason of which I can't figure out. </p>
  
  <p>The following code is a minimal crashing example, to be compiled and run with <code>mpic++ -std=c++11 -g -o test test.cpp &amp;&amp; ./test</code>:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;memory&gt;
  #include &lt;mpi.h&gt;
  
  class Environment {
  public:
      static Environment &amp;getInstance() {
          static Environment instance;
          return instance;
      }
  
      static bool initialized() {
          int ini;
          MPI_Initialized(&amp;ini);
          return ini != 0;
      }
  
      static bool finalized() {
          int fin;
          MPI_Finalized(&amp;fin);
          return fin != 0;
      }
  
  private:
      Environment() {
          if(!initialized()) {
              MPI_Init(NULL, NULL);
              _initialized = true;
          }
      }
  
      ~Environment() {
          if(!_initialized)
              return;
          if(finalized())
              return;
          MPI_Finalize();
      }
  
      bool _initialized{false};
  
  public:
      Environment(Environment const &amp;) = delete;
      void operator=(Environment const &amp;)  = delete;
  };
  
  class Status {
  private:
      std::shared_ptr&lt;MPI_Status&gt; _mpi_status;
      MPI_Datatype _mpi_type;
  };
  
  class Request { 
  private:
      std::shared_ptr&lt;MPI_Request&gt; _request;
      int _flag;
      Status _status;
  };
  
  int main() {
      auto &amp;m = Environment::getInstance();
  
      MPI_Request r;
      MPI_Status s;
      int a;
      MPI_Test(&amp;r, &amp;a, &amp;s);
  
      Request r2;
      printf("b\n");
  }
  </code></pre>
  
  <p>Basically, the <code>Environment</code> class is a singleton wrapper around <code>MPI_Init</code> and <code>MPI_Finalize</code>. When the program exits, MPI will be finalized and the first time the class is instantiated, <code>MPI_Init</code> is called. Then I do some MPI stuff in the <code>main()</code> function, involving some other simple wrapper objects. </p>
  
  <p>The code above crashes (on my machine, OpenMPI &amp; Linux). However, it works when I </p>
  
  <ul>
  <li>comment any of the private members of <code>Request</code> or <code>Status</code> (even <code>int _flag;</code>)</li>
  <li>comment the last line, <code>printf("b\n");</code></li>
  <li>Replace <code>auto &amp;m = Environment::getInstance();</code> with <code>MPI_Init()</code>.</li>
  </ul>
  
  <p>There doesn't seem to be a connection between these points and I have no clue where to look for the segmentation fault. </p>
  
  <p>The stack trace is:</p>
  
  <pre><code>[pc13090:05978] *** Process received signal ***
  [pc13090:05978] Signal: Segmentation fault (11)
  [pc13090:05978] Signal code: Address not mapped (1)
  [pc13090:05978] Failing at address: 0x61
  [pc13090:05978] [ 0] /usr/lib/libpthread.so.0(+0x11dd0)[0x7fa9cf818dd0]
  [pc13090:05978] [ 1] /usr/lib/openmpi/libmpi.so.40(ompi_request_default_test+0x16)[0x7fa9d0357326]
  [pc13090:05978] [ 2] /usr/lib/openmpi/libmpi.so.40(MPI_Test+0x31)[0x7fa9d03970b1]
  [pc13090:05978] [ 3] ./test(+0xb7ae)[0x55713d1aa7ae]
  [pc13090:05978] [ 4] /usr/lib/libc.so.6(__libc_start_main+0xea)[0x7fa9cf470f4a]
  [pc13090:05978] [ 5] ./test(+0xb5ea)[0x55713d1aa5ea]
  [pc13090:05978] *** End of error message ***
  -------------------------------------------------------
  Primary job  terminated normally, but 1 process returned
  a non-zero exit code. Per user-direction, the job has been aborted.
  -------------------------------------------------------
  --------------------------------------------------------------------------
  mpirun noticed that process rank 0 with PID 0 on node pc13090 exited on signal 11 (Segmentation fault).
  --------------------------------------------------------------------------
  </code></pre>