You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by bo...@apache.org on 2022/12/07 03:45:53 UTC

[pulsar] 02/02: [fix][cpp] Use weak ptr avoid circular references. (#17481)

This is an automated email from the ASF dual-hosted git repository.

bogong pushed a commit to branch branch-2.9
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 8036bd070f67e13da9ff8fbfef4d452c74d75479
Author: Baodi Shi <wu...@icloud.com>
AuthorDate: Thu Sep 22 15:49:12 2022 +0800

    [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.
    
    (cherry picked from commit 141981b902ae4727d6220055d9292d41c9f61b2e)
---
 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 5fe9446b186..c35d84b6db6 100644
--- a/pulsar-client-cpp/lib/MultiTopicsConsumerImpl.cc
+++ b/pulsar-client-cpp/lib/MultiTopicsConsumerImpl.cc
@@ -734,11 +734,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<MultiTopicsConsumerImpl> 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();
         }
     });