From 4c63d9a6344c565b56ded1612a587de53fc44463 Mon Sep 17 00:00:00 2001 From: Samuel Jackson Date: Wed, 28 Aug 2013 14:34:20 +0100 Subject: [PATCH] Fixed bug with data service creating multiple workspace references Refs #7807 --- .../Framework/API/src/WorkspaceGroup.cpp | 16 ++++++------ .../Kernel/inc/MantidKernel/DataService.h | 25 ++++--------------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/Code/Mantid/Framework/API/src/WorkspaceGroup.cpp b/Code/Mantid/Framework/API/src/WorkspaceGroup.cpp index 376f4bc77d8b..ac6ec718159c 100644 --- a/Code/Mantid/Framework/API/src/WorkspaceGroup.cpp +++ b/Code/Mantid/Framework/API/src/WorkspaceGroup.cpp @@ -290,15 +290,13 @@ void WorkspaceGroup::workspaceReplaceHandle(Mantid::API::WorkspaceBeforeReplaceN bool isObserving = m_observingADS; if ( isObserving ) observeADSNotifications( false ); - const std::string replacedName = notice->object_name(); - for(auto citr=m_workspaces.begin(); citr!=m_workspaces.end(); ++citr) - { - if ( (**citr).name() == replacedName ) - { - *citr = notice->new_object(); - break; - } - } + + //remove any workspace that already exists with the name + this->remove(notice->object_name()); + + //replace the object in the group + this->addWorkspace(notice->new_object()); + if ( isObserving ) observeADSNotifications( true ); } diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/DataService.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/DataService.h index b58a09d224df..5ea5c74d3f02 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/DataService.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/DataService.h @@ -278,7 +278,7 @@ class DLLExport DataService { checkForEmptyName(newName); - // Make DataService access thread-safe + auto item = this->retrieve(oldName); m_mutex.lock(); std::string foundName; @@ -290,29 +290,14 @@ class DLLExport DataService return; } - // delete the object with the old name + // delete the object with the old name silently auto object = it->second; - datamap.erase( it ); + datamap.erase(it); - // if there is another object which has newName delete it - it = datamap.find( newName ); - if ( it != datamap.end() ) - { - notificationCenter.postNotification(new AfterReplaceNotification(newName,object)); - datamap.erase( it ); - } + m_mutex.unlock(); - // insert the old object with the new name - if ( ! datamap.insert(typename svcmap::value_type(newName, object)).second ) - { - std::string error=" add : Unable to insert Data Object : '"+newName+"'"; - g_log.error(error); - m_mutex.unlock(); - throw std::runtime_error(error); - } - g_log.information("Data Object '"+ foundName +"' renamed to '" + newName + "'"); + this->addOrReplace(newName,item); - m_mutex.unlock(); notificationCenter.postNotification(new RenameNotification(oldName, newName)); return;