Boost logo

Boost Users :

From: Marc Magrans de Abril (marcmagransdeabril_at_[hidden])
Date: 2008-07-15 12:18:44


Dear colleagues,

I have been testing the boost interprocess library for two months in
my spare time... everything looked ok until now. Perhaps I am doing
something wrong, so be patient.

As you can see in the code, I am trying to use the offset_ptr within a
class named OffsetPtrClass. However, if I try to allocate the int
inside the OffsetPtrClass cnstructor I'll get a segmentation fault in
the getter (either the MASTER or the SLAVE forked processes).
I a running on Linux kernel 2.6.16 and g++ 3.4.xx.

What I am doing wrong?

Thanks for your help,
marc

#include <iostream>
#include <unistd.h>

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/offset_ptr.hpp>

namespace ip=boost::interprocess;

class OffsetPtrClass {
 public:
  OffsetPtrClass():myint_(0) {
    //If we allocate the int here we get a segmentation fault!!
    //
    //ip::managed_shared_memory segment(ip::open_only,"MySharedSegment");
    //myint_ = static_cast<int*>(segment.allocate(sizeof(int)));

  }

  ~OffsetPtrClass() {

  }

  void setMyint(const int& myint) {
    *myint_ = myint;
  }

  int getMyint() {
    int myint = *myint_;
    return myint;
  }

  ip::offset_ptr<int> myint_;
};

int main( int argc, char* argv[] ) {
  std::string shName("MySharedSegment");

  pid_t pid = fork();
  if (pid == 0) {
    try {
      std::cout << "CHILD: getpid() = " << getpid() << std::endl;

      std::cout << "CHILD: press return to continue..." <<std::endl;
      getchar();

      ip::managed_shared_memory segment(ip::open_only, shName.c_str());

      std::cout << "CHILD: Retrieving OffsetPtrClass 'optr' instance:
" << std::endl;
      std::pair<OffsetPtrClass*, std::size_t> res;
      res = segment.find<OffsetPtrClass> ("optr");
      assert(res.second == 1);
      OffsetPtrClass& optr =*(res.first);

      std::cout << "CHILD: 'optr'::getMyint() = " << optr.getMyint()
<< std::endl;
      std::cout << "CHILD: 'optr'::SetMyint(100)" << std::endl;
      optr.setMyint(100);

    } catch (...) {
      std::cerr << "CHILD: Something failed..." << std::endl;
      throw;
    }

  } else if (pid <0){
    std::cerr << "---program.cc: Failed to fork" << std::endl;
    exit(1);

  } else {
    std::cout << "PARENT: getpid() = " << getpid() << std::endl;

    ip::shared_memory_object::remove(shName.c_str());
    try{
      std::cout << "PARENT: creating segment" << std::endl;
      ip::managed_shared_memory segment(ip::create_only, shName.c_str(), 65536);

      std::cout << "PARENT: creating OffsetPtrClass 'optr' instance"
<< std::endl;
      OffsetPtrClass& optr = *(segment.construct<OffsetPtrClass>("optr")());

      //If we allocate the int here we do not have a segmentation fault
      optr.myint_ = static_cast<int*>(segment.allocate(sizeof(int)));

      std::cout << "PARENT: 'optr'::SetMyint(333)" << std::endl;
      optr.setMyint(333);
      std::cout << "PARENT: 'optr'::getMyint() = " << optr.getMyint()
<< std::endl;
      std::cout << "PARENT: 'optr'::*myint_() = " << *(optr.myint_) <<
std::endl;

      std::cout << "PARENT: press return to continue..." <<std::endl;
      getchar();

      std::cout << "PARENT: 'optr'::getMyint() = " << optr.getMyint()
<< std::endl;

    } catch(...){
      std::cerr << "PARENT:something failed... Everything is
destroyed" << std::endl;
      throw;
    }

    std::cout << "PARENT: Shared memory destroyed" << std::endl;
  }

}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net