I'm having a problem where I am using Boost Interprocess to write some values to shared memory using managed_shared_memory::construct() then in another process trying to read those values using managed_shared_memory::find() but it is not coming back with the same address that was created using construct().
I took the sample code from the Boost Interprocess documentation for Creating named shared memory objects and split into two different programs and the same thing is happening.
interprocess_write.cpp
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib> //std::system
#include <cstddef>
#include <cassert>
#include <iostream>
#include <utility>
int main(int argc, char *argv[])
{
using namespace boost::interprocess;
typedef std::pair<double, int> MyType;
if(argc == 1){ //Parent process
shared_memory_object::remove ("MySharedMemory");
//Construct managed shared memory
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
//Create an object of MyType initialized to {0.0, 0}
MyType *instance = segment.construct<MyType>
("MyType instance") //name of the object
(0.0, 0); //ctor first argument
//Create an array of 10 elements of MyType initialized to {0.0, 0}
MyType *array = segment.construct<MyType>
("MyType array") //name of the object
[10] //number of elements
(0.0, 0); //Same two ctor arguments for all objects
//Create an array of 3 elements of MyType initializing each one
//to a different value {0.0, 0}, {1.0, 1}, {2.0, 2}...
float float_initializer[3] = { 0.0, 1.0, 2.0 };
int int_initializer[3] = { 0, 1, 2 };
MyType *array_it = segment.construct_it<MyType>
("MyType array from it") //name of the object
[3] //number of elements
( &float_initializer[0] //Iterator for the 1st ctor argument
, &int_initializer[0]); //Iterator for the 2nd ctor argument
std::cout << array << ":" << instance << ":" << array_it << std::endl;
//Check child has destroyed all objects
if(segment.find<MyType>("MyType array").first ||
segment.find<MyType>("MyType instance").first ||
segment.find<MyType>("MyType array from it").first)
return 1;
}
return 0;
}
interprocess_read.cpp
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib> //std::system
#include <cstddef>
#include <cassert>
#include <iostream>
#include <utility>
int main (int argc, char **argv)
{
using namespace boost::interprocess;
typedef std::pair<double, int> MyType;
//Open managed shared memory
managed_shared_memory segment(open_only, "MySharedMemory");
std::pair<MyType*, managed_shared_memory::size_type> res;
//Find the array
res = segment.find<MyType> ("MyType array");
//Length should be 10
if(res.second != 10) return 1;
std::cout << res.first << ":";
//Find the object
res = segment.find<MyType> ("MyType instance");
//Length should be 1
if(res.second != 1) return 1;
std::cout << res.first << ":";
//Find the array constructed from iterators
res = segment.find<MyType> ("MyType array from it");
//Length should be 3
if(res.second != 3) return 1;
std::cout << res.first << std::endl;
//We're done, delete all the objects
segment.destroy<MyType>("MyType array");
segment.destroy<MyType>("MyType instance");
segment.destroy<MyType>("MyType array from it");
}
When I ran the interprocess_write program I got the following output:
0x1260128:0x12600d8:0x1260208
But when I run the interprocess_read program I get:
0x2ad0128:0x2ad00d8:0x2ad0208
Is there something missing that has to be done in interprocess_read to make it pull the correct addresses out of shared memory?