Another attempt to solve some strange bug
diff --git a/model/sync-ccnx-wrapper.cc b/model/sync-ccnx-wrapper.cc
index 58bacc4..aa7ab8c 100644
--- a/model/sync-ccnx-wrapper.cc
+++ b/model/sync-ccnx-wrapper.cc
@@ -38,10 +38,10 @@
   , m_running (true)
 {
   m_handle = ccn_create ();
+  initKeyStore ();
+  createKeyLocator ();
   if (ccn_connect(m_handle, NULL) < 0)
     BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
-  initKeyStore();
-  createKeyLocator();
   m_thread = thread (&CcnxWrapper::ccnLoop, this);
 }
 
@@ -55,9 +55,9 @@
   
   m_thread.join ();
   ccn_disconnect (m_handle);
+  ccn_destroy (&m_handle);
   ccn_charbuf_destroy (&m_keyLoactor);
   ccn_keystore_destroy (&m_keyStore);
-  ccn_destroy (&m_handle);
 }
 
 /// @cond include_hidden
@@ -107,26 +107,30 @@
 CcnxWrapper::ccnLoop ()
 {
   pollfd pfds[1];
-  int res = ccn_run(m_handle, 0);
 
-  pfds[0].fd = ccn_get_connection_fd(m_handle);
+  pfds[0].fd = ccn_get_connection_fd (m_handle);
   pfds[0].events = POLLIN;
 
   while (m_running)
     {
-      if (res >= 0)
-        {
-          int ret = poll(pfds, 1, 100);
-          if (ret < 0)
-            {
-              BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
-            }
+      int res = 0;
+      {
+        recursive_mutex::scoped_lock lock (m_mutex);
+        res = ccn_run (m_handle, 0);
+      }
 
-          recursive_mutex::scoped_lock lock(m_mutex);
-          if (!m_running) break;
-          
-          res = ccn_run(m_handle, 0);
+      if (!m_running) break;
+      
+      if (res < 0)
+        BOOST_THROW_EXCEPTION (CcnxOperationException()
+                               << errmsg_info_str("ccn_run returned error"));
+
+      int ret = poll(pfds, 1, 10);
+      if (ret < 0)
+        {
+          BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
         }
+
     }
 }
 
@@ -135,6 +139,10 @@
 int
 CcnxWrapper::publishData (const string &name, const string &dataBuffer, int freshness)
 {
+  recursive_mutex::scoped_lock lock(m_mutex);
+  if (!m_running)
+    return -1;
+  
   // cout << "Publish: " << name << endl;
   ccn_charbuf *pname = ccn_charbuf_create();
   ccn_charbuf *signed_info = ccn_charbuf_create();
@@ -154,13 +162,13 @@
 			   NULL, getPrivateKey()) < 0)
     BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
 
-  recursive_mutex::scoped_lock lock(m_mutex);
   if (ccn_put(m_handle, content->buf, content->length) < 0)
     BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
 
   ccn_charbuf_destroy (&pname);
   ccn_charbuf_destroy (&signed_info);
   ccn_charbuf_destroy (&content);
+  return 0;
 }
 
 
@@ -242,6 +250,10 @@
 
 int CcnxWrapper::sendInterest (const string &strInterest, const DataCallback &dataCallback)
 {
+  recursive_mutex::scoped_lock lock(m_mutex);
+  if (!m_running)
+    return -1;
+  
   // std::cout << "Send interests for " << strInterest << std::endl;
   ccn_charbuf *pname = ccn_charbuf_create();
   ccn_closure *dataClosure = new ccn_closure;
@@ -250,15 +262,19 @@
   dataClosure->data = new DataCallback (dataCallback); // should be removed when closure is removed
 
   dataClosure->p = &incomingData;
-  recursive_mutex::scoped_lock lock(m_mutex);
   if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
     BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
 
   ccn_charbuf_destroy (&pname);
+  return 0;
 }
 
 int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
 {
+  recursive_mutex::scoped_lock lock(m_mutex);
+  if (!m_running)
+    return -1;
+
   ccn_charbuf *pname = ccn_charbuf_create();
   ccn_closure *interestClosure = new ccn_closure;
 
@@ -277,6 +293,10 @@
 void
 CcnxWrapper::clearInterestFilter (const std::string &prefix)
 {
+  recursive_mutex::scoped_lock lock(m_mutex);
+  if (!m_running)
+    return;
+
   std::cout << "clearInterestFilter" << std::endl;
   ccn_charbuf *pname = ccn_charbuf_create();