I'm attempting to share a vector between two Windows processes which run in the same context (either both as user processes, or both as a service). I'm going by the official example which actually works just fine for me.
The only difference is that instead of a vector of int I use a vector of my own type of struct:
struct MyStatus
{
MyStatus() : lastSeen(0) {}
char field1[255];
char field2[41];
__int64 lastSeen;
friend bool operator< (const MyStatus& lhs, const MyStatus& rhs)
{
return stricmp(lhs.field1, rhs.field1);
}
friend bool operator> (const MyStatus& lhs, const MyStatus& rhs)
{
return stricmp(rhs.field1, lhs.field1);
}
};
Again, this worked ok in the sample when the process creating the shared memory calls the child process. However, now that I have 2 different processes, it doesn't work anymore since the "client" can't find the vector anymore.
The caller does something like this:
typedef boost::interprocess::allocator<MyStatus, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
typedef boost::interprocess::vector<MyStatus, ShmemAllocator> MyStatusVector;
shared_memory_object::remove("mysharedmemory");
managed_shared_memory segment(create_only, "mysharedmemory", 64 * sizeof(MyStatus));
const ShmemAllocator alloc_inst(segment.get_segment_manager());
// ptrMyStatus_ is a member of a class
// MyStatusVector* ptrMyStatus_
ptrMyStatus_ = segment.construct<MyStatusVector>("mysharedvector"(alloc_inst);
After the vector is constructed, I am successfully populating it in the process.
The issue arises when I try to find and iterate through the vector in the other process. I can open the shared memory segment without issues, but .find() always returns NULL:
managed_shared_memory segment(open_read_only, "mysharedmemory");
MyStatusVector *myvector = segment.find<MyStatusVector>("mysharedvector").first;
// myvector will always be NULL
Another issue is that when I open the shared memory segment in the client with "open_only", then the client process hangs indefinitely when trying to find the vector.
This is my first time dabbling with inter process communication, so I'm not exactly experienced. I don't understand why it's finding the shared memory, but not the vector inside the shared memory. Yet it worked in the sample.
So clearly I seem to be missing something substantial, I just don't know what.