Skip to content

Commit

Permalink
fixed dangling point in GetLatestErrorMessage. (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
ifplusor authored and ShannonDing committed Aug 26, 2019
1 parent 8a2ed5a commit e15bd83
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 39 deletions.
19 changes: 6 additions & 13 deletions src/common/MQClientErrorContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 9 additions & 14 deletions src/common/MQClientErrorContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
2 changes: 1 addition & 1 deletion src/extern/CErrorMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/extern/CProducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/extern/CPullConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/extern/CPushConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e15bd83

Please sign in to comment.