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


I have again a question to using the boost vector with shared memory
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{

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)();

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,

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at