I'm trying to get boost::interprocess to share memory between 32 bit and 64 bit processes. This bug tracker entry suggests that this might be possible in Boost 1.49, which is what I use.
As a test I tried sharing an unsigned int. It's a simple Qt application with two buttons.
#define SHARED_MEMORY_NAME "My shared memory"
#define SHARED_VAR_NAME "testVar"
namespace bip = boost::interprocess;
void on_createMemButton_clicked()
{
std::cout << "sizeof(unsigned int): " << sizeof(unsigned int) << std::endl;
bip::shared_memory_object::remove(SHARED_MEMORY_NAME);
bip::managed_shared_memory mem(bip::create_only, SHARED_MEMORY_NAME, 12345);
mem.construct<unsigned int>(SHARED_VAR_NAME)(42);
std::cout << "Created shared memory " << SHARED_MEMORY_NAME << std::endl;
}
void on_accessMemButton_clicked()
{
try
{
std::cout << "sizeof(unsigned int): " << sizeof(unsigned int) << std::endl;
bip::managed_shared_memory mem(bip::open_only, SHARED_MEMORY_NAME);
std::pair<unsigned int*, size_t> p = mem.find<unsigned int>(SHARED_VAR_NAME);
std::cout<< "got " << p.second << " numbers " << std::endl;
if (p.second > 0)
std::cout << "first number is: " << *p.first << std::endl;
bip::shared_memory_object::remove(SHARED_MEMORY_NAME);
}
catch (bip::interprocess_exception e)
{
std::cout << "Shared mem " << SHARED_MEMORY_NAME << " not found" << std::endl;
}
}
If I create or access the shared memory from processes with the same bitness it works without problems. But if I create the memory in a 64 bit process, and read from a 32 bit process, the process enters the managed_shared_memory::find()
function and never comes back. If I try it the other way round, the 64 bit process fails in managed_shared_memory::find()
again, this time with an access violation. Using managed_windows_shared_memory
yielded the same results.
Is there any way to make this work?
I had the same problem. I had a DLL running inside another process and a controller app compiled separately. My find() and find_or_construct() would hang, making it look like a dead lock. Changing to null_mutex_family did nothing.
The problem turned out to be the char type used to compile the DLL vs. the controller app. Setting both to use Multi-Byte chars fixed it for me (using MSVC).
I added this line to my code so that it will never, ever bite me like that again, right before accessing my managed_shared_memory instance.
If you try to find and object constructed with one type of char, from an app using another type, boost will hang in an endless loop (that I have not had the patience to try and find).