From 141981b902ae4727d6220055d9292d41c9f61b2e Mon Sep 17 00:00:00 2001 From: Baodi Shi Date: Thu, 22 Sep 2022 15:49:12 +0800 Subject: [PATCH] [fix][cpp] Use weak ptr avoid circular references. (#17481) ### Motivation Capturing shared ptr in the timer function will cause a circular reference. ### Modifications - Use weak ptr instead shared ptr. --- pulsar-client-cpp/lib/MultiTopicsConsumerImpl.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pulsar-client-cpp/lib/MultiTopicsConsumerImpl.cc b/pulsar-client-cpp/lib/MultiTopicsConsumerImpl.cc index 34d94a052ea9a..7515076234556 100644 --- a/pulsar-client-cpp/lib/MultiTopicsConsumerImpl.cc +++ b/pulsar-client-cpp/lib/MultiTopicsConsumerImpl.cc @@ -748,11 +748,12 @@ uint64_t MultiTopicsConsumerImpl::getNumberOfConnectedConsumer() { } void MultiTopicsConsumerImpl::runPartitionUpdateTask() { partitionsUpdateTimer_->expires_from_now(partitionsUpdateInterval_); - auto self = shared_from_this(); - partitionsUpdateTimer_->async_wait([self](const boost::system::error_code& ec) { + std::weak_ptr weakSelf{shared_from_this()}; + partitionsUpdateTimer_->async_wait([weakSelf](const boost::system::error_code& ec) { // If two requests call runPartitionUpdateTask at the same time, the timer will fail, and it // cannot continue at this time, and the request needs to be ignored. - if (!ec) { + auto self = weakSelf.lock(); + if (self && !ec) { self->topicPartitionUpdate(); } });