BewareMyPower commented on code in PR #17239:
URL: https://github.com/apache/pulsar/pull/17239#discussion_r953262625
##########
pulsar-client-cpp/lib/MultiTopicsConsumerImpl.cc:
##########
@@ -364,13 +366,47 @@ void MultiTopicsConsumerImpl::closeAsync(ResultCallback
callback) {
state_ = Closing;
- auto self = shared_from_this();
+ std::weak_ptr<MultiTopicsConsumerImpl> weakSelf{shared_from_this()};
Review Comment:
To avoid extending the lifetime of `MultiTopicsConsumerImpl`. e.g.
```c++
{
consumer.closeAsync(nullptr);
} // consumer is destroyed here
```
If we capture a shared pointer here in handleOneTopicSubscribed, the
callback will still be executed after the consumer is destroyed.
Actually, the best practice for `shared_ptr` and `weak_ptr` is to specify
the owner and only the owner holds a `shared_ptr`. If the owner wants to share
it to other objects, it should pass a `std::weak_ptr`. This best practice could
avoid cyclic dependency. (I might push another PR to follow this rule)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]