boost vector including non standard objects

Hello! I would like to create a vector in shared memory which includes objects of a class Listener*. Listener* was created by my own and is therefore no standard. When I try the following, using the boost interprocess library: typedef boost::interprocess::allocator<Listener*, managed_shared_memory::segment_manager> ShmemAllocator; typedef boost::interprocess::vector<Listener*, ShmemAllocator> MyVector; MyVector *ListenerVector; managed_shared_memory segment(create_only, "MySharedMemory", 65536); const ShmemAllocator alloc_inst(segment.get_segment_manager()); ListenerVector = segment.construct<MyVector>("ListenerVector")(alloc_inst); ListenerVector->push_back((Listener*)listener); The code works perfectly when I use a vector with integers, but it crashes when I use a vector with objects of my own class. I wanted to ask if it is possible to create a boost vector holding a non standard object and if so what I do wrong. Thank you very much for your help! Best regards, Sabine

El 04/01/2012 16:49, Sabine B escribió:
Hello!
I would like to create a vector in shared memory which includes objects of a class Listener*. Listener* was created by my own and is therefore no standard.
When I try the following, using the boost interprocess library: typedef boost::interprocess::allocator<Listener*, managed_shared_memory::segment_manager> ShmemAllocator; typedef boost::interprocess::vector<Listener*, ShmemAllocator> MyVector; MyVector *ListenerVector;
managed_shared_memory segment(create_only, "MySharedMemory", 65536); const ShmemAllocator alloc_inst(segment.get_segment_manager()); ListenerVector = segment.construct<MyVector>("ListenerVector")(alloc_inst);
ListenerVector->push_back((Listener*)listener);
You can't store raw pointers in shared memory but offset_ptr-s, it's explained in the docs: http://www.boost.org/doc/libs/1_41_0/doc/html/interprocess/sharedmemorybetwe... And you need to allocate listeners also in shared memory using construct (with anonymous name, see docs for details). Best, Ion
participants (2)
-
Ion Gaztañaga
-
Sabine B