0

I am trying to have two processes access the same shared memory. I have process1 creating a deque with my_custom_class*. my_custom_class contains some integers.

typedef boost::interprocess::allocator<my_custom_frame*, boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator;
typedef boost::interprocess::deque<my_custom_frame*, ShmemAllocator> VideoDeque;

boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only, "MySharedMemory", 100000000);
video_deque = segment.construct<VideoDeque>("VideoDeque")(alloc_inst);

After creating the deque in the shared memory using the custom allocator I create a my_custom_frame using the allocate method on the shared memory. I first allocate memory for the size of my_custom_frame and then using placement new() I create a my_custom_frame object there.

temp1 = segment.allocate(sizeof(my_custom_frame));
frame1 = (my_custom_frame*) new (temp1) my_custom_frame();

I then set the integers in my_custom_frame by calling a copy_dframe_to_cf method on frame1. I then push frame1 onto video_deque. At this point if I set a break point in my code frame1 has all the integer variables set up properly.

frame1->copy_dframe_to_cf(video_frames[0].get());
video_deque->push_back(frame1);

In process2 I open the shared memory and find my deque.

boost::interprocess::managed_shared_memory segment(boost::interprocess::open_only, "MySharedMemory");

//Find the vector using the c-string name
VideoDeque* my_video_deque = segment.find<VideoDeque>("VideoDeque").first;

I then get the my_custom_frame from my_video_deque.

typedef std::deque<my_custom_frame*> MyFrameQueue;
MyFrameQueue my_video_frames;

my_video_frames.push_back(my_video_deque->front());
my_custom_frame *pFrame;
pFrame = my_video_frames[0];

At this point when I look at the values in pFrame they are all set to 0. The memory address pFrame points to is the same I had put on the deque in process1 so I would like to think I am accessing the same correct memory location but for some reason the values are being reset. Any help would be appreciated.

4

1 回答 1

0

在共享内存中存储原始指针是禁忌:共享内存段不太可能在所有进程中映射到相同的地址。您需要根据boost.interprocess 文档使用 offset_ptr,或者只需按值而不是指针存储所有内容。

IE,

typedef boost::interprocess::deque<my_custom_frame, ShmemAllocator> VideoDeque;

或者

typedef boost::interprocess::deque<boost::interprocess::offset_ptr<my_custom_frame>, ShmemAllocator> VideoDeque;
于 2013-07-11T20:21:58.470 回答