This is an automated email from the ASF dual-hosted git repository.

dinglei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-cpp.git


The following commit(s) were added to refs/heads/master by this push:
     new e15bd83  fixed dangling point in GetLatestErrorMessage. (#181)
e15bd83 is described below

commit e15bd83d082edcb4d8a8bce166ebe704b137b4b8
Author: James Yin <[email protected]>
AuthorDate: Mon Aug 26 11:35:40 2019 +0800

    fixed dangling point in GetLatestErrorMessage. (#181)
---
 src/common/MQClientErrorContainer.cpp | 19 ++++++-------------
 src/common/MQClientErrorContainer.h   | 23 +++++++++--------------
 src/extern/CErrorMessage.cpp          |  2 +-
 src/extern/CProducer.cpp              | 12 ++++++------
 src/extern/CPullConsumer.cpp          |  8 ++++----
 src/extern/CPushConsumer.cpp          |  2 +-
 6 files changed, 27 insertions(+), 39 deletions(-)

diff --git a/src/common/MQClientErrorContainer.cpp 
b/src/common/MQClientErrorContainer.cpp
index 2fac383..a9e0e5d 100644
--- a/src/common/MQClientErrorContainer.cpp
+++ b/src/common/MQClientErrorContainer.cpp
@@ -14,25 +14,18 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 #include "MQClientErrorContainer.h"
 
 namespace rocketmq {
-MQClientErrorContainer* MQClientErrorContainer::s_instance = nullptr;
 
-MQClientErrorContainer* MQClientErrorContainer::instance() {
-  if (!s_instance)
-    s_instance = new MQClientErrorContainer();
-  return s_instance;
-}
+thread_local std::string MQClientErrorContainer::t_err;
 
-void MQClientErrorContainer::setErr(std::string str) {
-  boost::lock_guard<boost::mutex> lock(this->mutex);
-  this->m_err = str;
+void MQClientErrorContainer::setErr(const std::string& str) {
+  t_err = str;
 }
 
-std::string MQClientErrorContainer::getErr() {
-  boost::lock_guard<boost::mutex> lock(this->mutex);
-  return this->m_err;
+const std::string& MQClientErrorContainer::getErr() {
+  return t_err;
 }
+
 }  // namespace rocketmq
diff --git a/src/common/MQClientErrorContainer.h 
b/src/common/MQClientErrorContainer.h
index 9c92407..3e09f0a 100644
--- a/src/common/MQClientErrorContainer.h
+++ b/src/common/MQClientErrorContainer.h
@@ -14,28 +14,23 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#ifndef __MQ_CLIENT_ERROR_CONTAINER_H__
+#define __MQ_CLIENT_ERROR_CONTAINER_H__
 
-#ifndef __MQCLIENTERRORCONTAINER_H_INCLUDE__
-#define __MQCLIENTERRORCONTAINER_H_INCLUDE__
-#include <boost/thread/lock_guard.hpp>
-#include <boost/thread/mutex.hpp>
-#include <exception>
+#include <mutex>
 #include <string>
 
 namespace rocketmq {
+
 class MQClientErrorContainer {
  public:
-  static MQClientErrorContainer* instance();
-
-  void setErr(std::string str);
-
-  std::string getErr();
+  static void setErr(const std::string& str);
+  static const std::string& getErr();
 
  private:
-  std::string m_err;
-  static MQClientErrorContainer* s_instance;
-  boost::mutex mutex;
+  static thread_local std::string t_err;
 };
+
 }  // namespace rocketmq
 
-#endif
+#endif  // __MQ_CLIENT_ERROR_CONTAINER_H__
diff --git a/src/extern/CErrorMessage.cpp b/src/extern/CErrorMessage.cpp
index 1e858ad..420fbe2 100644
--- a/src/extern/CErrorMessage.cpp
+++ b/src/extern/CErrorMessage.cpp
@@ -25,7 +25,7 @@ extern "C" {
 using namespace rocketmq;
 
 const char* GetLatestErrorMessage() {
-  return MQClientErrorContainer::instance()->getErr().c_str();
+  return MQClientErrorContainer::getErr().c_str();
 }
 
 #ifdef __cplusplus
diff --git a/src/extern/CProducer.cpp b/src/extern/CProducer.cpp
index 792800d..28b43d4 100644
--- a/src/extern/CProducer.cpp
+++ b/src/extern/CProducer.cpp
@@ -100,7 +100,7 @@ int StartProducer(CProducer* producer) {
   try {
     ((DefaultMQProducer*)producer)->start();
   } catch (exception& e) {
-    MQClientErrorContainer::instance()->setErr(string(e.what()));
+    MQClientErrorContainer::setErr(string(e.what()));
     return PRODUCER_START_FAILED;
   }
   return OK;
@@ -156,7 +156,7 @@ int SendMessageSync(CProducer* producer, CMessage* msg, 
CSendResult* result) {
     strncpy(result->msgId, sendResult.getMsgId().c_str(), 
MAX_MESSAGE_ID_LENGTH - 1);
     result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0;
   } catch (exception& e) {
-    MQClientErrorContainer::instance()->setErr(string(e.what()));
+    MQClientErrorContainer::setErr(string(e.what()));
     return PRODUCER_SEND_SYNC_FAILED;
   }
   return OK;
@@ -219,7 +219,7 @@ int SendMessageAsync(CProducer* producer,
       delete cSendCallback;
       cSendCallback = NULL;
     }
-    MQClientErrorContainer::instance()->setErr(string(e.what()));
+    MQClientErrorContainer::setErr(string(e.what()));
     return PRODUCER_SEND_ASYNC_FAILED;
   }
   return OK;
@@ -249,7 +249,7 @@ int SendMessageOnewayOrderly(CProducer* producer, CMessage* 
msg, QueueSelectorCa
     SelectMessageQueue selectMessageQueue(selector);
     defaultMQProducer->sendOneway(*message, &selectMessageQueue, arg);
   } catch (exception& e) {
-    MQClientErrorContainer::instance()->setErr(string(e.what()));
+    MQClientErrorContainer::setErr(string(e.what()));
     return PRODUCER_SEND_ONEWAY_FAILED;
   }
   return OK;
@@ -276,7 +276,7 @@ int SendMessageOrderlyAsync(CProducer* producer,
   } catch (exception& e) {
     printf("%s\n", e.what());
     // std::count<<e.what( )<<std::endl;
-    MQClientErrorContainer::instance()->setErr(string(e.what()));
+    MQClientErrorContainer::setErr(string(e.what()));
     return PRODUCER_SEND_ORDERLYASYNC_FAILED;
   }
   return OK;
@@ -303,7 +303,7 @@ int SendMessageOrderly(CProducer* producer,
     strncpy(result->msgId, sendResult.getMsgId().c_str(), 
MAX_MESSAGE_ID_LENGTH - 1);
     result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0;
   } catch (exception& e) {
-    MQClientErrorContainer::instance()->setErr(string(e.what()));
+    MQClientErrorContainer::setErr(string(e.what()));
     return PRODUCER_SEND_ORDERLY_FAILED;
   }
   return OK;
diff --git a/src/extern/CPullConsumer.cpp b/src/extern/CPullConsumer.cpp
index 0e1c13b..198c11e 100644
--- a/src/extern/CPullConsumer.cpp
+++ b/src/extern/CPullConsumer.cpp
@@ -49,7 +49,7 @@ int StartPullConsumer(CPullConsumer* consumer) {
   try {
     ((DefaultMQPullConsumer*)consumer)->start();
   } catch (exception& e) {
-    MQClientErrorContainer::instance()->setErr(string(e.what()));
+    MQClientErrorContainer::setErr(string(e.what()));
     return PULLCONSUMER_START_FAILED;
   }
   return OK;
@@ -152,7 +152,7 @@ int FetchSubscriptionMessageQueues(CPullConsumer* consumer, 
const char* topic, C
   } catch (MQException& e) {
     *size = 0;
     *mqs = NULL;
-    MQClientErrorContainer::instance()->setErr(string(e.what()));
+    MQClientErrorContainer::setErr(string(e.what()));
     return PULLCONSUMER_FETCH_MQ_FAILED;
   }
   return OK;
@@ -177,7 +177,7 @@ CPullResult Pull(CPullConsumer* consumer,
   try {
     cppPullResult = ((DefaultMQPullConsumer*)consumer)->pull(messageQueue, 
subExpression, offset, maxNums);
   } catch (exception& e) {
-    MQClientErrorContainer::instance()->setErr(string(e.what()));
+    MQClientErrorContainer::setErr(string(e.what()));
     cppPullResult.pullStatus = BROKER_TIMEOUT;
   }
 
@@ -232,7 +232,7 @@ int ReleasePullResult(CPullResult pullResult) {
     try {
       delete ((PullResult*)pullResult.pData);
     } catch (exception& e) {
-      MQClientErrorContainer::instance()->setErr(string(e.what()));
+      MQClientErrorContainer::setErr(string(e.what()));
       return NULL_POINTER;
     }
   }
diff --git a/src/extern/CPushConsumer.cpp b/src/extern/CPushConsumer.cpp
index e45116b..621eabd 100644
--- a/src/extern/CPushConsumer.cpp
+++ b/src/extern/CPushConsumer.cpp
@@ -108,7 +108,7 @@ int StartPushConsumer(CPushConsumer* consumer) {
   try {
     ((DefaultMQPushConsumer*)consumer)->start();
   } catch (exception& e) {
-    MQClientErrorContainer::instance()->setErr(string(e.what()));
+    MQClientErrorContainer::setErr(string(e.what()));
     return PUSHCONSUMER_START_FAILED;
   }
   return OK;

Reply via email to