[pulsar-client-cpp] Fix memory leak caused by not being executed ClientConnection destructor #5286
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Currently, closing a C++ Pulsar client object does not execute the destructor for the generated
ClientConnection
object. As a result, memory usage increases in applications that repeatedly create and close clients.This pull-request is a continuation of #5246.
Modifications
The reason why the
ClientConnection
destructor is not executed is that the reference count of the shared pointer does not become 0 even when theClientConnection
object is closed. As a result of the investigation, it was found that if the shared pointerexecutor_
held by theClientConnection
object is reset, the reference count becomes 0 and the destructor is executed.However, with this fix alone, executing the following code causes an "Illegal instruction" error and the program crashes.
https://gist.github.com/massakam/ebd169caff3db802eee5001b0d06c980
The exact cause is unknown, but this issue doesn't seem to occur if closing
ClientConnection
before closingExecutorServiceProvider
. So, I changed theConnectionPool
destructor added in #5246 to theclose()
method and call it before closingExecutorServiceProvider
.