Copilot commented on code in PR #580:
URL: https://github.com/apache/pulsar-client-cpp/pull/580#discussion_r3297012540
##########
tests/TypedMessageTest.cc:
##########
@@ -65,15 +65,16 @@ TEST(TypedMessageTest, testReceive) {
ASSERT_EQ(ResultOk, consumer.receive(msg, 3000, intDecoder));
} else {
Latch latch{1};
- consumer.receiveAsync(
+ consumer.receiveAsync<int>(
[&latch, &msg, &msgMutex](Result result, const
TypedMessage<int>& receivedMsg) {
ASSERT_EQ(ResultOk, result);
{
std::lock_guard<std::mutex> lock{msgMutex};
- msg = TypedMessage<int>{receivedMsg, intDecoder};
+ msg = receivedMsg;
}
latch.countdown();
- });
+ },
+ intDecoder);
Review Comment:
Passing a lambda directly to `Consumer::receiveAsync<T>` binds it to the
overload that takes `const std::function<...>&`. In `Consumer.h`, that callback
reference is captured by the internal lambda passed to the untyped
`receiveAsync`, so if the argument here is a temporary `std::function` (created
from the lambda), it can dangle once this call returns (UB if the receive
completes asynchronously). To make the test robust, store the callback in an
lvalue `std::function` that stays alive until `latch` is released, and pass
that variable to `receiveAsync<int>()`.
--
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]