Boost logo

Boost Users :

Subject: [Boost-users] extract objects from vector in shared memory
From: Sabine B (sabine565_at_[hidden])
Date: 2012-01-09 07:04:09


Hello!

I have again a question to using the boost vector with shared memory
objects.
I managed inserting the objects of type "MyType" into the vector with the
following code:

typedef boost::interprocess::allocator<MyType*,
managed_shared_memory::segment_manager> ShmemAllocator;
typedef boost::interprocess::vector<MyType*, ShmemAllocator> MyVector;

struct shm_remove{
   shm_remove(){shared_memory_object::remove("MySharedMemory");}
   ~shm_remove(){shared_memory_object::remove("MySharedMemory");}
}remover;

managed_shared_memory segment(create_only, "MySharedMemory", 65536);

const ShmemAllocator alloc_inst(segment.get_segment_manager());
MyVector *vector = segment.construct<MyVector>("Vector")(alloc_inst);

MyType *mytype = segment.construct<MyType>(anonymous_instance)();
MyVector->push_back(mytype);

The problem is that I can't manage to extract the object from the vector. I
always get a null pointer or
a pointer to somewhere.
This is the code for the child process:

managed_shared_memory segment(open_only, "MySharedMemory");
vector = segment.find<MyVector>("Vector").first;
MyType *mytype = vector->at(1); // program crashes when I try to print
members of the object MyType or
MyType mytype = (MyType&)vector->at(1); // object is empty

Does anybody have any hint or an idea what I'm doing wrong?

Thank you very much for your help!

Best regards,
Sabine



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