From ab693f1f649c497f9d0b00e2491ed23eb26a7c86 Mon Sep 17 00:00:00 2001 From: cc9cii Date: Fri, 5 Dec 2014 07:50:03 +1100 Subject: [PATCH] Workaround file lock being lost if the same file is closed elsewhere in the program (see https://svn.boost.org/trac/boost/ticket/3582) --- apps/opencs/editor.cpp | 11 ++++++----- apps/opencs/editor.hpp | 2 ++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index 9fe974d86..f609b80b7 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -72,8 +72,10 @@ CS::Editor::Editor (OgreInit::OgreInit& ogreInit) CS::Editor::~Editor () { + mPidFile.close(); + if(mServer && boost::filesystem::exists(mPid)) - remove(mPid.string().c_str()); // ignore error + remove(mPid.string().c_str()); // ignore any error // cleanup global resources used by OEngine delete OEngine::Physic::BulletShapeManager::getSingletonPtr(); @@ -246,7 +248,7 @@ bool CS::Editor::makeIPCServer() mPid /= "opencs.pid"; bool pidExists = boost::filesystem::exists(mPid); - boost::filesystem::ofstream tempFile(mPid); + mPidFile.open(mPid); mLock = boost::interprocess::file_lock(mPid.string().c_str()); if(!mLock.try_lock()) @@ -256,11 +258,10 @@ bool CS::Editor::makeIPCServer() } #ifdef _WIN32 - tempFile << GetCurrentProcessId() << std::endl; + mPidFile << GetCurrentProcessId() << std::endl; #else - tempFile << getpid() << std::endl; + mPidFile << getpid() << std::endl; #endif - tempFile.close(); mServer = new QLocalServer(this); diff --git a/apps/opencs/editor.hpp b/apps/opencs/editor.hpp index 30a031309..273f0825b 100644 --- a/apps/opencs/editor.hpp +++ b/apps/opencs/editor.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -56,6 +57,7 @@ namespace CS boost::filesystem::path mResources; boost::filesystem::path mPid; boost::interprocess::file_lock mLock; + boost::filesystem::ofstream mPidFile; bool mFsStrict; void setupDataFiles (const Files::PathContainer& dataDirs);