Search code examples
c++boostxulrunnerboost-interprocess

Boost.Interprocess - created managed_shared_memory could not be found


I'm about to use shared memory for interprocess communication (obviously ;) between a xulrunner extension (component) and a plugin (NPAPI). Both are written in C++!

I was following the instructions of the boost lib document

Now: The creation of the shared memory is working fine by doing this:

struct shm_remove
   {
       shm_remove() { NS_BI::shared_memory_object::remove("MySharedMemory"); }
      ~shm_remove() { NS_BI::shared_memory_object::remove("MySharedMemory"); }
   } remover;


   _myShMemSegment = NS_BI::managed_shared_memory( NS_BI::open_or_create, "MySharedMemory", 65536 ) ;
   SHMEM_ALLOCATOR alloc_inst( _myShMemSegment.get_segment_manager() ) ;

   _pMyShMemMap = _myShMemSegment.construct<SHMEM_MAP>("cgfMap")
                                                        (std::less<int>()
                                                        , alloc_inst) ;

The path

C:\Users\All Users\boost_interprocess\<aNumber>\

shows me a cryptic named file, during the process is running. By closing the program, this file gets deleted. So far so good.

I try to open this shared memory in another process with:

NS_BI::managed_shared_memory( NS_BI::open_read_only, "MySharedMemory") ;

but then it throws the boost::interprocess::interprocess_exception System cannot find the file specified

If I do

NS_BI::managed_shared_memory( NS_BI::open_or_create, "MySharedMemory", 1024) ;

I get a second file in the Users path in the exact same subdirectory. So why couldn't the first one be found by the consumer process?

Btw: If I'm trying to open the file in the path with a texteditor (notepad++, etc), it couldn't because it asserts that the file doesn't exist.

Thanks for your time!!!


Solution

  • Here is the solution:

    struct shm_remove
    {
        shm_remove() { NS_BI::shared_memory_object::remove("MySharedMemory"); }
       ~shm_remove() { NS_BI::shared_memory_object::remove("MySharedMemory"); }
    } remover;
    

    is the original code from the boost documentation. In case of the example that worked great. But in my case (to be honest I'm not sure WHY) I have to call

    NS_BI::shared_memory_object::remove("MySharedMemory");
    

    right before to build up the shared memory.

    You should not forget to do the same in your destructor to delete it!