boost::interprocess_exception - library_error exce

2019-09-11 18:34发布

问题:

In some rare cases (in fact on a single client's computer) code below throws an exception "library_error":

namespace ipc = boost::interprocess;
ipc::shared_memory_object m_shm;
...
bool initAsServer(size_t sharedMemSize)
{
    ipc::permissions perm;
    perm.set_unrestricted();

    try
    {
        m_shm = ipc::shared_memory_object(
            ipc::create_only,
            CNameGenHelper::genUniqueNameUtf8().c_str(), // static std::string genUniqueNameUtf8()
            ipc::read_write, perm);
    }
    catch(const ipc::interprocess_exception& ex)
    {
        logError("failed with exception \"%s\"", ex.what());
        return false;
    }
    ...
}

In log file: [ERR] failed with exception "boost::interprocess_exception::library_error"

Boost v1.58, platform win32, vs13.


I'll be very grateful if you help me in solving this problem. Thank you in advance!

回答1:

Reason of problem is events with Event ID = "6005" and source name is "EventLog" in "System" Windows log. Event Viewer - Windows Logs - System. If the system log does not contain at least one such event, then method boost::interprocess::winapi::get_last_bootup_time() returns false and boost::interprocess::ipcdetail::windows_bootstamp constructor throws exception. (define BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME is used).

So it seems that it is enough to clear the "System" windows event log and any application that uses the Boost shared memory will stop working.

What a terrible logic: use the contents of the windows event log. It seems this boost ipc implementation bug that has not yet been fixed (boost_1_61_0).

My temporary workaround for this case (w/o reboot of computer):

bool fixBoostIpcSharedMem6005issue() const
{
    bool result = false;

    HANDLE hEventLog = ::RegisterEventSourceA(NULL, "EventLog");
    if(hEventLog)
    {
        const char* msg = "simple boost shared memory fix for 6005";

        if(::ReportEventA(hEventLog, EVENTLOG_INFORMATION_TYPE, 0, 6005, NULL, 1, 0, &msg, NULL))
            result = true;

        ::DeregisterEventSource(hEventLog);
    }

    return result;
}

Use it and try to use ipc::shared_memory_object again :)



回答2:

Many detailed explanations about by the problem, by one of the authors of the library: Boost interprocess: Getting boot-up time is unreliable on Windows and here: Interprocess get_last_bootup_time use of Event Log on Windows is completely unreliable

Apparently, a reliable solution is to define the preprocessor constant BOOST_INTERPROCESS_SHARED_DIR_PATH to a function call, which always returns the same directory path as a string, once the machine is booted. For example by formatting the update time-stamp of a file, written to at start-up.