Boost logo

Boost Users :

Subject: Re: [Boost-users] How to Read the stored memory map in C++
From: Roland Bock (rbock_at_[hidden])
Date: 2009-09-28 02:21:44


manish4gupta wrote:
> Previously i was doing in the same way but still getting the same error. I
> was removing he shared region in process2 only but pblm is still there.Pls
> can i get any working example doing the same.
> Process1. storing map<string, int> nad process2 reading it.I will really be
> thankful to you.
>
>
> Christoph Gysin-3 wrote:
>> 2009/9/25 manish4gupta <manish_at_[hidden]>:
>>> How can i solve my pblm? I tried many things but nothing seems to be
>>> working.
>> Calling shared_memory_object::remove("MySharedMemory") removes the
>> whole shared memory where you saved your data. If it gets called
>> before Process2 tries to read it, it will fail with mentioned
>> interprocess_exception.
>>
>> Just don't remove it in Process1, remove it at the end of Process2.
>>
>> Chris
>> --

What Christoph wrote was correct:

In the code you posted on th 18th, process1 removes the shared memory on
exit (when the object called "remover" goes out of scope). Thus, when
you run process1 it brings the shared memory into being, writes the map
into it and then removes the whole stuff again.

Thus, when you start process2 afterwards, the shared memory is long
gone. Hence the exception.

So in the code posted on the 18th, you just have to replace the remover
by the following line:

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

That works.

BTW: In a "real world" application the remover would be a very useful
helper class. If you do not know why, take a good book on C++, for
instance "Effective C++" by Scott Meyers, and read until you do know :-)

Regards,

Roland

PS: And please(!) remove the unnecessary stuff, before posting. The
complex_data class for instance is totally irrelevant. Same goes for
most of the typedefs. All this nonsense just adds to the noise which
makes it harder to see the actual problem.

PPS: Attached, please find the reduced code of process1.




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