title: using boost::interprocess in class object segfaults, why?
body: <p>I'm trying to get some code working that uses a class to set up and manage shared memory, and call an object of that class from my main prog. All the code works when it is directly in my main(), but when I set it up in a class, and instantiate an object of that class, I get a segfault.</p>
  
  <p>I don't know why or how to fix it?</p>
  
  <p>First example here shows the example working without problem in my main() function:</p>
  
  <pre><code>#include &lt;boost/interprocess/managed_shared_memory.hpp&gt;
  using namespace boost::interprocess;
  
  typedef struct teststruct {
      int     testint=9;
  };
  
  typedef std::pair&lt;teststruct, int&gt; memsh_teststruct;
  
  int main(int argc, char *argv[])
  {
  
  
      printf("inMain0\n");
  
  
      struct shm_remove
      {
          shm_remove() { shared_memory_object::remove("testShare"); }
          ~shm_remove(){ shared_memory_object::remove("testShare"); }
      } remover;
  
      //Construct managed shared memory
      managed_shared_memory segment(create_only, "testShare", 65536);
  
  
      teststruct myTeststruct;
      memsh_teststruct *inst_teststruct;
  
      inst_teststruct = segment.construct&lt;memsh_teststruct&gt;
          ("name_myTeststruct") 
          (myTeststruct, 0); 
  
      printf("construct_ptr: %p \n", &amp;inst_teststruct-&gt;first);
  
      inst_teststruct-&gt;first.testint = 1234;
  
      printf("construct_val: %d\n", inst_teststruct-&gt;first.testint);
  
  
  
  
      int mainInt;
  
      printf("inMain1\n");
  
      mainInt = inst_teststruct-&gt;first.testint;
  
      printf("mainInt: %d\n", mainInt);
  
      printf("inMain2\n");
  }
  </code></pre>
  
  <p>The output looks fine, like this:</p>
  
  <pre><code>construct_ptr: 0x7f0d41834118 
  construct_val: 1234
  inMain0
  inMain1
  mainInt: 1234
  inMain2
  </code></pre>
  
  <p>Here's the second example that sets up the exact same shared memory, but using a class.</p>
  
  <pre><code>#include &lt;boost/interprocess/managed_shared_memory.hpp&gt;
  using namespace boost::interprocess;
  
  typedef struct teststruct {
      int     testint=9;
  };
  
  
  typedef std::pair&lt;teststruct, int&gt; memsh_teststruct;
  
  class testclass{
      public:
          testclass();
          bool something(int in);
  
          teststruct myTeststruct;
          memsh_teststruct *inst_teststruct;
  };
  
  
  
  testclass::testclass() 
  {
      struct shm_remove
      {
          shm_remove() { shared_memory_object::remove("testShare"); }
          ~shm_remove(){ shared_memory_object::remove("testShare"); }
      } remover;
  
      //Construct managed shared memory
      managed_shared_memory segment(create_only, "testShare", 65536);
  
      inst_teststruct = segment.construct&lt;memsh_teststruct&gt;
          ("name_myTeststruct") 
          (myTeststruct, 0); 
  
      printf("construct_ptr: %p \n", &amp;inst_teststruct-&gt;first);
  
      inst_teststruct-&gt;first.testint = 1234;
  
      printf("construct_val: %d\n", inst_teststruct-&gt;first.testint);
  }
  
  
  
  int main(int argc, char *argv[])
  {
  
      printf("inMain0\n");
  
      int mainInt;
      testclass testclassObj;
  
      printf("inMain1\n");
  
      mainInt = testclassObj.inst_teststruct-&gt;first.testint;
  
      printf("mainInt: %d\n", mainInt);
  
      printf("inMain2\n");
  }
  </code></pre>
  
  <p>But this second example segfaults, and this is the output:</p>
  
  <pre><code>inMain0
  construct_ptr: 0x7fa222d37118 
  construct_val: 1234
  inMain1
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>... So why does the call to </p>
  
  <pre><code>mainInt = testclassObj.inst_teststruct-&gt;first.testint;
  </code></pre>
  
  <p>from main() cause a segfault?</p>
  
  <p>I've also tried some other variations, such as defining other functions in my class to interact with the shared memory variables, and it also segfaults. </p>
  
  <p>If I had to guess what is going on, I am feeling like the shared memory is getting closed or something before I'm expecting it to, probably after exiting the testclass() constructor. But, I don't know the proper way to avoid this, so that the shared memory instead gets closed when returning from main() when the whole testclassObj object is cleaned up.</p>
  
  <p>On the other hand, maybe I'm wrong altogether?</p>
  
  <p>Thanks,
  B</p>
  
  <p>EDIT:
  EDIT2: last edit removed, my error was irrelevant and due to something dumb i did in regard to comment thread with Sean in his answer.</p>