fixed dangling point in GetLatestErrorMessage. (#181)
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(const std::string& str) {
+ t_err = str;
}
-void MQClientErrorContainer::setErr(std::string str) {
- boost::lock_guard<boost::mutex> lock(this->mutex);
- this->m_err = str;
+const std::string& MQClientErrorContainer::getErr() {
+ return t_err;
}
-std::string MQClientErrorContainer::getErr() {
- boost::lock_guard<boost::mutex> lock(this->mutex);
- return this->m_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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
} 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 @@
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 @@
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 @@
} 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 @@
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 @@
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 @@
try {
((DefaultMQPushConsumer*)consumer)->start();
} catch (exception& e) {
- MQClientErrorContainer::instance()->setErr(string(e.what()));
+ MQClientErrorContainer::setErr(string(e.what()));
return PUSHCONSUMER_START_FAILED;
}
return OK;