From f4aa3aecd9dae19ff652824ba397d6921365476a Mon Sep 17 00:00:00 2001
From: Francois Keith <keith@lirmm.fr>
Date: Wed, 12 Feb 2014 01:52:02 +0100
Subject: [PATCH] Correct rosPublish.rm method.

Deregister the signal formerly added.
Add a mutex system to prevent removing a signal while triggering it,
 to avoid a crash (especially if the topic was listened to).
---
 src/ros_publish.cpp | 9 +++++++++
 src/ros_publish.hh  | 2 ++
 2 files changed, 11 insertions(+)

diff --git a/src/ros_publish.cpp b/src/ros_publish.cpp
index 489f596..cef0afa 100644
--- a/src/ros_publish.cpp
+++ b/src/ros_publish.cpp
@@ -203,7 +203,14 @@ namespace dynamicgraph
 
   void RosPublish::rm (const std::string& signal)
   {
+    if(bindedSignal_.find(signal) == bindedSignal_.end())
+      return;
+
+    //lock the mutex to avoid deleting the signal during a call to trigger
+    while(! mutex_.try_lock() ){}
     bindedSignal_.erase (signal);
+    signalDeregistration(signal);
+    mutex_.unlock();
   }
 
   std::string RosPublish::list () const
@@ -230,11 +237,13 @@ namespace dynamicgraph
     if (dt < rate_)
       return dummy;
 
+    while(! mutex_.try_lock() ){}
     for (iterator_t it = bindedSignal_.begin ();
 	 it != bindedSignal_.end (); ++it)
       {
 	boost::get<1>(it->second) (t);
       }
+    mutex_.unlock();
     return dummy;
   }
 
diff --git a/src/ros_publish.hh b/src/ros_publish.hh
index 3a6d5d4..5cb5ce2 100644
--- a/src/ros_publish.hh
+++ b/src/ros_publish.hh
@@ -5,6 +5,7 @@
 
 # include <boost/shared_ptr.hpp>
 # include <boost/tuple/tuple.hpp>
+# include <boost/interprocess/sync/interprocess_mutex.hpp>
 
 # include <dynamic-graph/entity.h>
 # include <dynamic-graph/signal-time-dependent.h>
@@ -94,6 +95,7 @@ namespace dynamicgraph
     dynamicgraph::SignalTimeDependent<int,int> trigger_;
     ros::Duration rate_;
     ros::Time lastPublicated_;
+    boost::interprocess::interprocess_mutex mutex_;
   };
 } // end of namespace dynamicgraph.
 
-- 
GitLab