Skip to content

Commit e15bd83

Browse files
ifplusorShannonDing
authored andcommitted
fixed dangling point in GetLatestErrorMessage. (#181)
1 parent 8a2ed5a commit e15bd83

6 files changed

+27
-39
lines changed

src/common/MQClientErrorContainer.cpp

+6-13
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,18 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
1817
#include "MQClientErrorContainer.h"
1918

2019
namespace rocketmq {
21-
MQClientErrorContainer* MQClientErrorContainer::s_instance = nullptr;
2220

23-
MQClientErrorContainer* MQClientErrorContainer::instance() {
24-
if (!s_instance)
25-
s_instance = new MQClientErrorContainer();
26-
return s_instance;
27-
}
21+
thread_local std::string MQClientErrorContainer::t_err;
2822

29-
void MQClientErrorContainer::setErr(std::string str) {
30-
boost::lock_guard<boost::mutex> lock(this->mutex);
31-
this->m_err = str;
23+
void MQClientErrorContainer::setErr(const std::string& str) {
24+
t_err = str;
3225
}
3326

34-
std::string MQClientErrorContainer::getErr() {
35-
boost::lock_guard<boost::mutex> lock(this->mutex);
36-
return this->m_err;
27+
const std::string& MQClientErrorContainer::getErr() {
28+
return t_err;
3729
}
30+
3831
} // namespace rocketmq

src/common/MQClientErrorContainer.h

+9-14
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,23 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
#ifndef __MQ_CLIENT_ERROR_CONTAINER_H__
18+
#define __MQ_CLIENT_ERROR_CONTAINER_H__
1719

18-
#ifndef __MQCLIENTERRORCONTAINER_H_INCLUDE__
19-
#define __MQCLIENTERRORCONTAINER_H_INCLUDE__
20-
#include <boost/thread/lock_guard.hpp>
21-
#include <boost/thread/mutex.hpp>
22-
#include <exception>
20+
#include <mutex>
2321
#include <string>
2422

2523
namespace rocketmq {
24+
2625
class MQClientErrorContainer {
2726
public:
28-
static MQClientErrorContainer* instance();
29-
30-
void setErr(std::string str);
31-
32-
std::string getErr();
27+
static void setErr(const std::string& str);
28+
static const std::string& getErr();
3329

3430
private:
35-
std::string m_err;
36-
static MQClientErrorContainer* s_instance;
37-
boost::mutex mutex;
31+
static thread_local std::string t_err;
3832
};
33+
3934
} // namespace rocketmq
4035

41-
#endif
36+
#endif // __MQ_CLIENT_ERROR_CONTAINER_H__

src/extern/CErrorMessage.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern "C" {
2525
using namespace rocketmq;
2626

2727
const char* GetLatestErrorMessage() {
28-
return MQClientErrorContainer::instance()->getErr().c_str();
28+
return MQClientErrorContainer::getErr().c_str();
2929
}
3030

3131
#ifdef __cplusplus

src/extern/CProducer.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ int StartProducer(CProducer* producer) {
100100
try {
101101
((DefaultMQProducer*)producer)->start();
102102
} catch (exception& e) {
103-
MQClientErrorContainer::instance()->setErr(string(e.what()));
103+
MQClientErrorContainer::setErr(string(e.what()));
104104
return PRODUCER_START_FAILED;
105105
}
106106
return OK;
@@ -156,7 +156,7 @@ int SendMessageSync(CProducer* producer, CMessage* msg, CSendResult* result) {
156156
strncpy(result->msgId, sendResult.getMsgId().c_str(), MAX_MESSAGE_ID_LENGTH - 1);
157157
result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0;
158158
} catch (exception& e) {
159-
MQClientErrorContainer::instance()->setErr(string(e.what()));
159+
MQClientErrorContainer::setErr(string(e.what()));
160160
return PRODUCER_SEND_SYNC_FAILED;
161161
}
162162
return OK;
@@ -219,7 +219,7 @@ int SendMessageAsync(CProducer* producer,
219219
delete cSendCallback;
220220
cSendCallback = NULL;
221221
}
222-
MQClientErrorContainer::instance()->setErr(string(e.what()));
222+
MQClientErrorContainer::setErr(string(e.what()));
223223
return PRODUCER_SEND_ASYNC_FAILED;
224224
}
225225
return OK;
@@ -249,7 +249,7 @@ int SendMessageOnewayOrderly(CProducer* producer, CMessage* msg, QueueSelectorCa
249249
SelectMessageQueue selectMessageQueue(selector);
250250
defaultMQProducer->sendOneway(*message, &selectMessageQueue, arg);
251251
} catch (exception& e) {
252-
MQClientErrorContainer::instance()->setErr(string(e.what()));
252+
MQClientErrorContainer::setErr(string(e.what()));
253253
return PRODUCER_SEND_ONEWAY_FAILED;
254254
}
255255
return OK;
@@ -276,7 +276,7 @@ int SendMessageOrderlyAsync(CProducer* producer,
276276
} catch (exception& e) {
277277
printf("%s\n", e.what());
278278
// std::count<<e.what( )<<std::endl;
279-
MQClientErrorContainer::instance()->setErr(string(e.what()));
279+
MQClientErrorContainer::setErr(string(e.what()));
280280
return PRODUCER_SEND_ORDERLYASYNC_FAILED;
281281
}
282282
return OK;
@@ -303,7 +303,7 @@ int SendMessageOrderly(CProducer* producer,
303303
strncpy(result->msgId, sendResult.getMsgId().c_str(), MAX_MESSAGE_ID_LENGTH - 1);
304304
result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0;
305305
} catch (exception& e) {
306-
MQClientErrorContainer::instance()->setErr(string(e.what()));
306+
MQClientErrorContainer::setErr(string(e.what()));
307307
return PRODUCER_SEND_ORDERLY_FAILED;
308308
}
309309
return OK;

src/extern/CPullConsumer.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int StartPullConsumer(CPullConsumer* consumer) {
4949
try {
5050
((DefaultMQPullConsumer*)consumer)->start();
5151
} catch (exception& e) {
52-
MQClientErrorContainer::instance()->setErr(string(e.what()));
52+
MQClientErrorContainer::setErr(string(e.what()));
5353
return PULLCONSUMER_START_FAILED;
5454
}
5555
return OK;
@@ -152,7 +152,7 @@ int FetchSubscriptionMessageQueues(CPullConsumer* consumer, const char* topic, C
152152
} catch (MQException& e) {
153153
*size = 0;
154154
*mqs = NULL;
155-
MQClientErrorContainer::instance()->setErr(string(e.what()));
155+
MQClientErrorContainer::setErr(string(e.what()));
156156
return PULLCONSUMER_FETCH_MQ_FAILED;
157157
}
158158
return OK;
@@ -177,7 +177,7 @@ CPullResult Pull(CPullConsumer* consumer,
177177
try {
178178
cppPullResult = ((DefaultMQPullConsumer*)consumer)->pull(messageQueue, subExpression, offset, maxNums);
179179
} catch (exception& e) {
180-
MQClientErrorContainer::instance()->setErr(string(e.what()));
180+
MQClientErrorContainer::setErr(string(e.what()));
181181
cppPullResult.pullStatus = BROKER_TIMEOUT;
182182
}
183183

@@ -232,7 +232,7 @@ int ReleasePullResult(CPullResult pullResult) {
232232
try {
233233
delete ((PullResult*)pullResult.pData);
234234
} catch (exception& e) {
235-
MQClientErrorContainer::instance()->setErr(string(e.what()));
235+
MQClientErrorContainer::setErr(string(e.what()));
236236
return NULL_POINTER;
237237
}
238238
}

src/extern/CPushConsumer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ int StartPushConsumer(CPushConsumer* consumer) {
108108
try {
109109
((DefaultMQPushConsumer*)consumer)->start();
110110
} catch (exception& e) {
111-
MQClientErrorContainer::instance()->setErr(string(e.what()));
111+
MQClientErrorContainer::setErr(string(e.what()));
112112
return PUSHCONSUMER_START_FAILED;
113113
}
114114
return OK;

0 commit comments

Comments
 (0)