0

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.

Lucky Luke
  • 1,373
  • 2
  • 13
  • 18
  • After sleeping on the issue and subsequent trial & error, I figured out that the issue has to do with sharing memory between a 64-bit and a 32-bit process, something I had overlooked. The shared memory & vector are created on a 64-bit process, yet it's a 32-bit process which is trying to access the vector. I'm not sure at this point if sharing memory is possible between different platforms. – Lucky Luke Apr 07 '16 at 13:07
  • Interaction between 64-bit and 32-bit processes using shared memory is possible, see [this](http://stackoverflow.com/questions/31696326/boost-interprocess-win32-x64/31882345#31882345) – doqtor Apr 07 '16 at 13:31
  • Interesting - thanks for clarifying. Unfortunately that largely defeats the purpose of that library - I was hoping to utilize vectors and such. At this point I suspect that I can just the windows native APIs to share memory. – Lucky Luke Apr 07 '16 at 14:07

0 Answers0