Boost logo

Boost Users :

Subject: Re: [Boost-users] boost vector including non standard objects
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2012-01-04 12:48:00


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/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations

And you need to allocate listeners also in shared memory using construct
(with anonymous name, see docs for details).

Best,

Ion


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