
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; } }

Marc Magrans de Abril wrote:
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?
I don't quite understand what your code is doing but first you should have created the shared memory before trying to open it. This code does not guarantee it. Another option is to create the shared memory first and fork after it. If you try to allocate the int in the master while constructing OffsetPtrClass in shared memory using a different ip::managed_shared_memory, are you sure that does not throw?
Thanks for your help, marc
I need a bit more data to help you, Ion

Dear Ion, First of all thanks for your fast reply. I have modified the code as suggested by you. The segmentation fault has disappeared. I do not want to be a pest... however, Could you please explain me why there was a segmentation fault before? I know that the way I synchronized was not good before doing the example. However, from the cout I supposed that the segment was created before pressing a key (If you take a look to the code). I neither understand why I was able to execute find<> without an exception. Could you please give me some more explanation? This is the code that works... #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"); { 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 << " Somthing failed..." << std::endl; } } pid_t pid = fork(); if (pid == 0) { try { std::cout << "CHILD: getpid() = " << getpid() << std::endl; 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; } } Thank you very much, marc PS: I have not replied directly to the message you send because I had the "digest" mode on. Sorry for the inconvenience.
participants (2)
-
Ion Gaztañaga
-
Marc Magrans de Abril