Skip to content

Commit 555e19d

Browse files
authored
feat(memleak): remove mem leak in factory schedule task. (#227)
1 parent b50d99c commit 555e19d

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

src/MQClientFactory.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ void MQClientFactory::start() {
8383
}
8484
}
8585

86-
void MQClientFactory::updateTopicRouteInfo(boost::system::error_code& ec, boost::asio::deadline_timer* t) {
86+
void MQClientFactory::updateTopicRouteInfo(boost::system::error_code& ec,
87+
boost::shared_ptr<boost::asio::deadline_timer> t) {
8788
if ((getConsumerTableSize() == 0) && (getProducerTableSize() == 0)) {
8889
return;
8990
}
@@ -784,7 +785,8 @@ void MQClientFactory::sendHeartbeatToAllBroker() {
784785
brokerTable.clear();
785786
}
786787

787-
void MQClientFactory::persistAllConsumerOffset(boost::system::error_code& ec, boost::asio::deadline_timer* t) {
788+
void MQClientFactory::persistAllConsumerOffset(boost::system::error_code& ec,
789+
boost::shared_ptr<boost::asio::deadline_timer> t) {
788790
{
789791
boost::lock_guard<boost::recursive_mutex> lock(m_consumerTableMutex);
790792
if (m_consumerTable.size() > 0) {
@@ -814,23 +816,26 @@ HeartbeatData* MQClientFactory::prepareHeartbeatData() {
814816
return pHeartbeatData;
815817
}
816818

817-
void MQClientFactory::timerCB_sendHeartbeatToAllBroker(boost::system::error_code& ec, boost::asio::deadline_timer* t) {
819+
void MQClientFactory::timerCB_sendHeartbeatToAllBroker(boost::system::error_code& ec,
820+
boost::shared_ptr<boost::asio::deadline_timer> t) {
818821
sendHeartbeatToAllBroker();
819822

820823
boost::system::error_code e;
821824
t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(30), e);
822825
t->async_wait(boost::bind(&MQClientFactory::timerCB_sendHeartbeatToAllBroker, this, ec, t));
823826
}
824827

825-
void MQClientFactory::timerCB_cleanOfflineBrokers(boost::system::error_code& ec, boost::asio::deadline_timer* t) {
828+
void MQClientFactory::timerCB_cleanOfflineBrokers(boost::system::error_code& ec,
829+
boost::shared_ptr<boost::asio::deadline_timer> t) {
826830
cleanOfflineBrokers();
827831

828832
boost::system::error_code e;
829833
t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(30), e);
830834
t->async_wait(boost::bind(&MQClientFactory::timerCB_cleanOfflineBrokers, this, ec, t));
831835
}
832836

833-
void MQClientFactory::fetchNameServerAddr(boost::system::error_code& ec, boost::asio::deadline_timer* t) {
837+
void MQClientFactory::fetchNameServerAddr(boost::system::error_code& ec,
838+
boost::shared_ptr<boost::asio::deadline_timer> t) {
834839
m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain);
835840

836841
boost::system::error_code e;
@@ -845,21 +850,24 @@ void MQClientFactory::startScheduledTask(bool startFetchNSService) {
845850
// callback
846851

847852
boost::system::error_code ec1;
848-
boost::asio::deadline_timer t1(m_async_ioService, boost::posix_time::seconds(3));
849-
t1.async_wait(boost::bind(&MQClientFactory::updateTopicRouteInfo, this, ec1, &t1));
853+
boost::shared_ptr<boost::asio::deadline_timer> t1 =
854+
boost::make_shared<boost::asio::deadline_timer>(m_async_ioService, boost::posix_time::seconds(3));
855+
t1->async_wait(boost::bind(&MQClientFactory::updateTopicRouteInfo, this, ec1, t1));
850856

851857
boost::system::error_code ec2;
852-
boost::asio::deadline_timer t2(m_async_ioService, boost::posix_time::milliseconds(10));
853-
t2.async_wait(boost::bind(&MQClientFactory::timerCB_sendHeartbeatToAllBroker, this, ec2, &t2));
858+
boost::shared_ptr<boost::asio::deadline_timer> t2 =
859+
boost::make_shared<boost::asio::deadline_timer>(m_async_ioService, boost::posix_time::milliseconds(10));
860+
t2->async_wait(boost::bind(&MQClientFactory::timerCB_sendHeartbeatToAllBroker, this, ec2, t2));
854861

855862
boost::system::error_code ec3;
856-
boost::asio::deadline_timer t3(m_async_ioService, boost::posix_time::seconds(3));
857-
t3.async_wait(boost::bind(&MQClientFactory::timerCB_cleanOfflineBrokers, this, ec3, &t3));
863+
boost::shared_ptr<boost::asio::deadline_timer> t3 =
864+
boost::make_shared<boost::asio::deadline_timer>(m_async_ioService, boost::posix_time::seconds(3));
865+
t3->async_wait(boost::bind(&MQClientFactory::timerCB_cleanOfflineBrokers, this, ec3, t3));
858866

859867
if (startFetchNSService) {
860868
boost::system::error_code ec5;
861-
boost::asio::deadline_timer* t5 =
862-
new boost::asio::deadline_timer(m_async_ioService, boost::posix_time::seconds(60 * 2));
869+
boost::shared_ptr<boost::asio::deadline_timer> t5 =
870+
boost::make_shared<boost::asio::deadline_timer>(m_async_ioService, boost::posix_time::seconds(60 * 2));
863871
t5->async_wait(boost::bind(&MQClientFactory::fetchNameServerAddr, this, ec5, t5));
864872
}
865873

@@ -885,19 +893,22 @@ void MQClientFactory::consumer_timerOperation() {
885893
// callback
886894

887895
boost::system::error_code ec1;
888-
boost::asio::deadline_timer t(m_consumer_async_ioService, boost::posix_time::seconds(10));
889-
t.async_wait(boost::bind(&MQClientFactory::timerCB_doRebalance, this, ec1, &t));
896+
boost::shared_ptr<boost::asio::deadline_timer> t1 =
897+
boost::make_shared<boost::asio::deadline_timer>(m_consumer_async_ioService, boost::posix_time::seconds(10));
898+
t1->async_wait(boost::bind(&MQClientFactory::timerCB_doRebalance, this, ec1, t1));
890899

891900
boost::system::error_code ec2;
892-
boost::asio::deadline_timer t2(m_consumer_async_ioService, boost::posix_time::seconds(5));
893-
t2.async_wait(boost::bind(&MQClientFactory::persistAllConsumerOffset, this, ec2, &t2));
901+
boost::shared_ptr<boost::asio::deadline_timer> t2 =
902+
boost::make_shared<boost::asio::deadline_timer>(m_consumer_async_ioService, boost::posix_time::seconds(5));
903+
t2->async_wait(boost::bind(&MQClientFactory::persistAllConsumerOffset, this, ec2, t2));
894904

895905
boost::system::error_code ec;
896906
m_consumer_async_ioService.run(ec);
897907
LOG_INFO("clientFactory:%s stop consumer_timerOperation", m_clientId.c_str());
898908
}
899909

900-
void MQClientFactory::timerCB_doRebalance(boost::system::error_code& ec, boost::asio::deadline_timer* t) {
910+
void MQClientFactory::timerCB_doRebalance(boost::system::error_code& ec,
911+
boost::shared_ptr<boost::asio::deadline_timer> t) {
901912
doRebalance();
902913

903914
boost::system::error_code e;

src/MQClientFactory.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,18 @@ class MQClientFactory {
129129

130130
void startScheduledTask(bool startFetchNSService = true);
131131
//<!timer async callback
132-
void fetchNameServerAddr(boost::system::error_code& ec, boost::asio::deadline_timer* t);
133-
void updateTopicRouteInfo(boost::system::error_code& ec, boost::asio::deadline_timer* t);
134-
void timerCB_sendHeartbeatToAllBroker(boost::system::error_code& ec, boost::asio::deadline_timer* t);
132+
void fetchNameServerAddr(boost::system::error_code& ec, boost::shared_ptr<boost::asio::deadline_timer> t);
133+
void updateTopicRouteInfo(boost::system::error_code& ec, boost::shared_ptr<boost::asio::deadline_timer> t);
134+
void timerCB_sendHeartbeatToAllBroker(boost::system::error_code& ec,
135+
boost::shared_ptr<boost::asio::deadline_timer> t);
135136

136-
void timerCB_cleanOfflineBrokers(boost::system::error_code& ec, boost::asio::deadline_timer* t);
137+
void timerCB_cleanOfflineBrokers(boost::system::error_code& ec, boost::shared_ptr<boost::asio::deadline_timer> t);
137138

138139
// consumer related operation
139140
void consumer_timerOperation();
140-
void persistAllConsumerOffset(boost::system::error_code& ec, boost::asio::deadline_timer* t);
141+
void persistAllConsumerOffset(boost::system::error_code& ec, boost::shared_ptr<boost::asio::deadline_timer> t);
141142
void doRebalance();
142-
void timerCB_doRebalance(boost::system::error_code& ec, boost::asio::deadline_timer* t);
143+
void timerCB_doRebalance(boost::system::error_code& ec, boost::shared_ptr<boost::asio::deadline_timer> t);
143144
bool getSessionCredentialFromConsumerTable(SessionCredentials& sessionCredentials);
144145
bool addConsumerToTable(const string& consumerName, MQConsumer* pMQConsumer);
145146
void eraseConsumerFromTable(const string& consumerName);

src/common/MQClient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ void MQClient::start() {
150150
}
151151

152152
void MQClient::shutdown() {
153+
m_clientFactory->shutdown();
153154
m_clientFactory = NULL;
154155
}
155156

0 commit comments

Comments
 (0)