Search code examples
c++boostshared-memory

Boost shared memory can't be initialized


Hi and there is my issue:

I'm trying to create a shared memory segment using this code snippet:

      #include <boost/interprocess/managed_shared_memory.hpp>
      using namespace boost::interprocess;

      ...   

      shared_memory_object::remove("MyShareMem");
      try {

        managed_shared_memory segment_(create_only,
                                       "MyShareMem",
                                       10 * 1024 * 1024);
        ...
      }
      catch (interprocess_exception &ex) {
        std::cout << "Exception: "<< ex.what() << std::endl;
      }

But then I've got an exception:

Exception: boost::interprocess::intermodule_singleton initialization failed

I am rather at a loss of what to do and how to fix the issue.

Is there any ideas how to deal with this?


UPDATED:

I found solution here boost::interprocess_exception - library_error exception when creating shared_memory_object

It's a bit weird, but here boost relies on Windows Event Logger. And if there is no event with ID = 6005. It throws an exception.

The code snippet from the boost 1.62.0 win32_api.hpp

//Obtains the bootup time from the System Event Log,
//event ID == 6005 (event log started).
//Adapted from http://msdn.microsoft.com/en-us/library/windows/desktop/bb427356.aspx
inline bool get_last_bootup_time(std::string &stamp)
{
   const char *source_name = "System";
   const char *provider_name = "EventLog";
   const unsigned short event_id = 6005u;

The solution was just to add the event 6005 and all work again fine.


Solution

  • I found the explanation here, why the windows system event logger is used. From the boost docs Shared memory emulation folder

    Shared memory (shared_memory_object) is implemented in windows using memory mapped files, placed in a shared directory in the shared documents folder (SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Common AppData). This directory name is the last bootup time obtained via COM calls (if BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME) defined or searching the system log for a startup event (the default implementation), so that each bootup shared memory is created in a new folder obtaining kernel persistence shared memory.

    If using BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME, due to COM implementation related errors, in Boost 1.48 & Boost 1.49 the bootup-time folder was dumped and files were directly created in shared documents folder, reverting to filesystem persistence shared memory. Boost 1.50 fixed those issues and recovered bootup time directory and kernel persistence. If you need to reproduce Boost 1.48 & Boost 1.49 behaviour to communicate with applications compiled with that version, comment #define BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME directive in the Windows configuration part of boost/interprocess/detail/workaround.hpp.

    If using the default implementation, (BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME undefined) and the Startup Event is not found, this might be due to some buggy software that floods or erases the event log.

    In any error case (shared documents folder is not defined or bootup time could not be obtained, the library throws an error. You still can use Boost.Interprocess definining your own directory as the shared directory. Just define BOOST_INTERPROCESS_SHARED_DIR_PATH when using the library and that path will be used to place shared memory files.

    And also there you can find a better solution of the issue. You just need to set your own folder where to store boost shared memory files by defining BOOST_INTERPROCESS_SHARED_DIR_PATH. And you won't be dependent on the windows event logger.