The code below creates and reads a file called MyMappedFile
. If I run the write code on Linux (Ubuntu 14.04), I can copy this file to other Linux machines and it reads fine. If I instead transfer it to OSX and attempt to run the read code on it, I get an error:
libc++abi.dylib: terminating with uncaught exception of type boost::interprocess::lock_exception: boost::interprocess::lock_exception
Conversely, files written on OSX are able to be read on OSX. But attempting to read them under Linux gives me a similar message:
terminate called after throwing an instance of 'boost::interprocess::lock_exception'
what(): boost::interprocess::lock_exception
The resulting files are different.
I'm writing with this code:
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/string.hpp>
namespace bip=boost::interprocess;
typedef bip::managed_shared_memory::segment_manager segment_manager_t;
typedef bip::basic_string<char, std::char_traits<char>, bip::allocator<char, segment_manager_t> > shared_string;
int main() {
bip::managed_mapped_file *outfile = new bip::managed_mapped_file(bip::create_only, "MyMappedFile", 1000);
outfile->find_or_construct<shared_string>("mystring")("bubza", outfile->get_segment_manager());
outfile->flush();
}
And reading with this:
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/string.hpp>
namespace bip=boost::interprocess;
typedef bip::managed_shared_memory::segment_manager segment_manager_t;
typedef bip::basic_string<char, std::char_traits<char>, bip::allocator<char, segment_manager_t> > shared_string;
int main() {
bip::managed_mapped_file *infile = new bip::managed_mapped_file(bip::open_only, "MyMappedFile");
shared_string *ptr = infile->find_or_construct<shared_string>("mystring")(infile->get_segment_manager());
std::cout << "I got " << *ptr << std::endl;
}
Both machines are using GCC 4.8 (4.8.4 on Linux, 4.8.5 on OSX) and Boost 1.55.
Why aren't the files compatible between these platforms?
Please note that I'm talking about the MyMappedFile
data file produced by the writing code. The executables produced by compiling the code are not being transferred between machines: the Linux code is compiled on a Linux machine, and the OSX code is compiled on the OSX machine.