Skip to content

Commit

Permalink
[fix][cpp] Use weak ptr avoid circular references. (apache#17481)
Browse files Browse the repository at this point in the history
### Motivation

Capturing shared ptr in the timer function will cause a circular reference.

### Modifications

- Use weak ptr instead shared ptr.
  • Loading branch information
shibd authored Sep 22, 2022
1 parent d9c9d73 commit 141981b
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pulsar-client-cpp/lib/MultiTopicsConsumerImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<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();
}
});
Expand Down

0 comments on commit 141981b

Please sign in to comment.