From 6193ae92095448f33b81c74bb9ac6fb892fb0ec3 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Fri, 12 May 2017 01:53:37 +0200 Subject: [PATCH] logic: Fixes for shared memory * set unrestricted permissions on shared memory allocation to fix crash on Windows * log exceptions during shared memory creation and destruction * create SharedMemoryGarbageCollector after logging was enabled --- src/lib/Application.cpp | 3 +- src/lib/utility/interprocess/SharedMemory.cpp | 95 ++++++++++++------- 2 files changed, 62 insertions(+), 36 deletions(-) diff --git a/src/lib/Application.cpp b/src/lib/Application.cpp index 8e7c61e0..9847d488 100644 --- a/src/lib/Application.cpp +++ b/src/lib/Application.cpp @@ -31,9 +31,10 @@ void Application::createInstance( const Version& version, ViewFactory* viewFactory, NetworkFactory* networkFactory ){ Version::setApplicationVersion(version); - SharedMemoryGarbageCollector::createInstance()->run(Application::getUUID()); loadSettings(); + SharedMemoryGarbageCollector::createInstance()->run(Application::getUUID()); + TaskScheduler::getInstance(); MessageQueue::getInstance(); diff --git a/src/lib/utility/interprocess/SharedMemory.cpp b/src/lib/utility/interprocess/SharedMemory.cpp index c4ed1449..ece0effc 100644 --- a/src/lib/utility/interprocess/SharedMemory.cpp +++ b/src/lib/utility/interprocess/SharedMemory.cpp @@ -67,58 +67,83 @@ SharedMemory::SharedMemory(const std::string& name, size_t initialMemorySize, Ac : m_name(checkName(name)) , m_mode(mode) { - bool unlockMutex = true; - - switch (mode) + try { - case CREATE_AND_DELETE: + bool unlockMutex = true; + + switch (mode) { - SharedMemoryGarbageCollector* collector = SharedMemoryGarbageCollector::getInstance(); - if (collector) + case CREATE_AND_DELETE: { - collector->registerSharedMemory(m_name); + SharedMemoryGarbageCollector* collector = SharedMemoryGarbageCollector::getInstance(); + if (collector) + { + collector->registerSharedMemory(m_name); + } + + deleteSharedMemory(m_name); + + boost::interprocess::permissions permissions; + permissions.set_unrestricted(); + + boost::interprocess::managed_shared_memory( + boost::interprocess::create_only, getMemoryName().c_str(), initialMemorySize, 0, permissions); + boost::interprocess::named_mutex(boost::interprocess::create_only, getMutexName().c_str()); } + break; + + case OPEN_ONLY: + boost::interprocess::managed_shared_memory( + boost::interprocess::open_only, getMemoryName().c_str()); + boost::interprocess::named_mutex(boost::interprocess::open_only, getMutexName().c_str()); + unlockMutex = false; + break; + + case OPEN_OR_CREATE: + { + boost::interprocess::permissions permissions; + permissions.set_unrestricted(); + + boost::interprocess::managed_shared_memory( + boost::interprocess::open_or_create, getMemoryName().c_str(), initialMemorySize, 0, permissions); + boost::interprocess::named_mutex(boost::interprocess::open_or_create, getMutexName().c_str()); + } + break; } - deleteSharedMemory(m_name); + if (unlockMutex) + { + boost::interprocess::named_mutex mutex(boost::interprocess::open_only, getMutexName().c_str()); + boost::interprocess::scoped_lock lock(mutex, boost::interprocess::try_to_lock); + } - boost::interprocess::managed_shared_memory( - boost::interprocess::create_only, getMemoryName().c_str(), initialMemorySize); - boost::interprocess::named_mutex(boost::interprocess::create_only, getMutexName().c_str()); - break; - - case OPEN_ONLY: - boost::interprocess::managed_shared_memory( - boost::interprocess::open_only, getMemoryName().c_str()); - boost::interprocess::named_mutex(boost::interprocess::open_only, getMutexName().c_str()); - unlockMutex = false; - break; - - case OPEN_OR_CREATE: - boost::interprocess::managed_shared_memory( - boost::interprocess::open_or_create, getMemoryName().c_str(), initialMemorySize); - boost::interprocess::named_mutex(boost::interprocess::open_or_create, getMutexName().c_str()); - break; } - - if (unlockMutex) + catch (boost::interprocess::interprocess_exception& e) { - boost::interprocess::named_mutex mutex(boost::interprocess::open_only, getMutexName().c_str()); - boost::interprocess::scoped_lock lock(mutex, boost::interprocess::try_to_lock); + LOG_ERROR_STREAM(<< "boost exception thrown at shared momory creation - " << getMemoryName() << ": " << e.what()); + throw e; } } SharedMemory::~SharedMemory() { - if (m_mode == CREATE_AND_DELETE) + try { - SharedMemoryGarbageCollector* collector = SharedMemoryGarbageCollector::getInstance(); - if (collector) + if (m_mode == CREATE_AND_DELETE) { - collector->unregisterSharedMemory(m_name); - } + SharedMemoryGarbageCollector* collector = SharedMemoryGarbageCollector::getInstance(); + if (collector) + { + collector->unregisterSharedMemory(m_name); + } - deleteSharedMemory(m_name); + deleteSharedMemory(m_name); + } + } + catch (boost::interprocess::interprocess_exception& e) + { + LOG_ERROR_STREAM(<< "boost exception thrown at shared momory destruction - " << getMemoryName() << ": " << e.what()); + throw e; } }