From 865c3d196d96a1092f16ae50d99c0094bc918faa Mon Sep 17 00:00:00 2001
From: Mirko Waechter <mirko.waechter@kit.edu>
Date: Sun, 21 Dec 2014 02:15:55 +0100
Subject: [PATCH] fixed clean up of RobotStateComponent (exception in
 destructor..)

---
 .../robotstate/RobotStateComponent.cpp        | 14 +++++++----
 .../robotstate/SharedRobotServants.cpp        | 23 +++++++++++++------
 .../robotstate/SharedRobotServants.h          |  2 +-
 3 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/source/RobotAPI/libraries/robotstate/RobotStateComponent.cpp b/source/RobotAPI/libraries/robotstate/RobotStateComponent.cpp
index 7da8f699f..90b55b02a 100644
--- a/source/RobotAPI/libraries/robotstate/RobotStateComponent.cpp
+++ b/source/RobotAPI/libraries/robotstate/RobotStateComponent.cpp
@@ -42,11 +42,15 @@ namespace armarx
 {
     RobotStateComponent::~RobotStateComponent()
     {
-        if (_synchronizedPrx)
+        try
         {
-            _synchronizedPrx->unref();
+            if (_synchronizedPrx)
+            {
+                _synchronizedPrx->unref();
+            }
         }
-        ARMARX_VERBOSE << "Destructor";
+        catch(...)
+        {}
     }
 
 
@@ -119,7 +123,7 @@ namespace armarx
         {
             SharedRobotInterfacePtr shared =  new SharedRobotServant(this->_synchronized);
             shared->ref();
-            this->_synchronizedPrx = SharedRobotInterfacePrx::uncheckedCast(current.adapter->addWithUUID(shared));
+            this->_synchronizedPrx = SharedRobotInterfacePrx::uncheckedCast(getObjectAdapter()->addWithUUID(shared));
         }
         return this->_synchronizedPrx;
     }
@@ -128,7 +132,7 @@ namespace armarx
     SharedRobotInterfacePrx RobotStateComponent::getRobotSnapshot(const string & time,const Current & current)
     {
         SharedRobotInterfacePtr p = new SharedRobotServant(this->_synchronized->clone(time));
-        return SharedRobotInterfacePrx::uncheckedCast(current.adapter->addWithUUID(p));
+        return SharedRobotInterfacePrx::uncheckedCast(getObjectAdapter()->addWithUUID(p));
     }
 
     void RobotStateComponent::reportJointAngles(const NameValueMap& jointAngles, bool aValueChanged, const Current& c)
diff --git a/source/RobotAPI/libraries/robotstate/SharedRobotServants.cpp b/source/RobotAPI/libraries/robotstate/SharedRobotServants.cpp
index c7cd866aa..61658211e 100644
--- a/source/RobotAPI/libraries/robotstate/SharedRobotServants.cpp
+++ b/source/RobotAPI/libraries/robotstate/SharedRobotServants.cpp
@@ -75,7 +75,8 @@ void SharedObjectBase::destroy(const Current &c)
     #ifdef VERBOSE
         ARMARX_ERROR_S << "[SharedObject] destroy " << " " << this << flush;
     #endif
-    } catch (const NotRegisteredException&){
+    } catch (const NotRegisteredException&e){
+//        ARMARX_INFO_S << "destroy failed with: " << e.what();
         throw ObjectNotExistException(__FILE__, __LINE__);
     };
 }
@@ -96,7 +97,7 @@ SharedRobotNodeServant::SharedRobotNodeServant(RobotNodePtr node) :
 SharedRobotNodeServant::~SharedRobotNodeServant()
 {
 #ifdef VERBOSE
-    ARMARX_LOG_S << "[SharedRobotNodeServant] destruct " << " " << this << flush;
+    ARMARX_FATAL_S << "[SharedRobotNodeServant] destruct " << " " << this << flush;
 #endif
 }
 
@@ -240,8 +241,14 @@ SharedRobotServant::~SharedRobotServant()
 #endif
     boost::recursive_mutex::scoped_lock cloneLock(m);
 
-    BOOST_FOREACH(NodeCache::value_type value, this->_cachedNodes)
-        value.second->unref();
+    for(auto value: this->_cachedNodes)
+    {
+        try
+        {
+            value.second->unref();
+        }
+        catch(...){}
+    }
 }
 
 SharedRobotNodeInterfacePrx SharedRobotServant::getRobotNode(const string &name, const Current &current)
@@ -249,7 +256,7 @@ SharedRobotNodeInterfacePrx SharedRobotServant::getRobotNode(const string &name,
 //    ARMARX_LOG_S << "Looking for node: " << name << flush;
     assert(_robot);
     boost::recursive_mutex::scoped_lock cloneLock(m);
-
+    SharedRobotNodeInterfacePrx prx;
     if (this->_cachedNodes.find(name) == this->_cachedNodes.end()){
         RobotNodePtr robotNode = _robot->getRobotNode(name);
         if (!robotNode){
@@ -260,8 +267,10 @@ SharedRobotNodeInterfacePrx SharedRobotServant::getRobotNode(const string &name,
         SharedRobotNodeInterfacePtr servant = new SharedRobotNodeServant(
             _robot->getRobotNode(name));
         //servant->ref();
-        this->_cachedNodes[name] = SharedRobotNodeInterfacePrx::uncheckedCast(current.adapter->addWithUUID(servant));
-        this->_cachedNodes[name]->ref();
+        prx = SharedRobotNodeInterfacePrx::uncheckedCast(current.adapter->addWithUUID(servant));
+        prx->ref();
+//        return prx;
+        this->_cachedNodes[name] = prx;
     }
     return this->_cachedNodes[name];
 }
diff --git a/source/RobotAPI/libraries/robotstate/SharedRobotServants.h b/source/RobotAPI/libraries/robotstate/SharedRobotServants.h
index 49f42dda5..4bdc97931 100644
--- a/source/RobotAPI/libraries/robotstate/SharedRobotServants.h
+++ b/source/RobotAPI/libraries/robotstate/SharedRobotServants.h
@@ -32,7 +32,7 @@ namespace armarx {
         public:
             virtual void ref(const Ice::Current &c);
             virtual void unref(const Ice::Current &c);
-            virtual void destroy(const Ice::Current &c);
+            void destroy(const Ice::Current &c);
             SharedObjectBase();
         private:
             unsigned int _referenceCount;
-- 
GitLab