diff --git a/slsDetectorSoftware/src/DetectorImpl.cpp b/slsDetectorSoftware/src/DetectorImpl.cpp index d75edce8e..ed1d07622 100644 --- a/slsDetectorSoftware/src/DetectorImpl.cpp +++ b/slsDetectorSoftware/src/DetectorImpl.cpp @@ -253,14 +253,15 @@ void DetectorImpl::setVirtualDetectorServers(const int numdet, const int port) { } void DetectorImpl::setHostname(const std::vector &name) { - // this check is there only to allow the previous detsizechan command - if (shm()->totalNumberOfModules != 0) { + // do not free always to allow the previous detsize/ initialchecks command + if (shm.exists() && shm()->totalNumberOfModules != 0) { LOG(logWARNING) << "There are already module(s) in shared memory." "Freeing Shared memory now."; - bool initialChecks = shm()->initialChecks; freeSharedMemory(); + } + // could be called after freeing shm from API + if (!shm.exists()) { setupDetector(); - shm()->initialChecks = initialChecks; } for (const auto &hostname : name) { addModule(hostname); diff --git a/slsDetectorSoftware/src/SharedMemory.h b/slsDetectorSoftware/src/SharedMemory.h index e31fc5fb9..a8e436664 100644 --- a/slsDetectorSoftware/src/SharedMemory.h +++ b/slsDetectorSoftware/src/SharedMemory.h @@ -33,7 +33,7 @@ namespace sls { template class SharedMemory { static constexpr int NAME_MAX_LENGTH = 255; std::string name; - T *shared_struct{}; + T *shared_struct{nullptr}; public: // moduleid of -1 creates a detector only shared memory @@ -64,8 +64,18 @@ template class SharedMemory { unmapSharedMemory(); } - T *operator()() { return shared_struct; } - const T *operator()() const { return shared_struct; } + T *operator()() { + if (shared_struct) + return shared_struct; + throw SharedMemoryError(getNoShmAccessMessage()); + } + + const T *operator()() const { + if (shared_struct) + return shared_struct; + throw SharedMemoryError(getNoShmAccessMessage()); + } + std::string getName() const { return name; } bool exists() { @@ -204,6 +214,11 @@ template class SharedMemory { throw SharedMemoryError(msg); } } + + const char* getNoShmAccessMessage() const { + return ("No shared memory to access. Create it first with " + "hostname or config command."); + }; }; } // namespace sls