Fix EXC_BAD_ACCESS crash when calling payload delegate methods.

Objective-C objects cannot be captured by reference in C++ lambdas and must always be captured by value.

This also takes a copy of the data returned from the Wi-Fi LAN reader, to prevent potential intermittent empty packet issues.

Fixes: #1702
PiperOrigin-RevId: 533925980
This commit is contained in:
Nick Bourdakos
2023-05-21 19:19:18 -07:00
committed by Copybara-Service
parent c9884f6218
commit 3c72e4f40c
2 changed files with 3 additions and 3 deletions
@@ -278,13 +278,13 @@ GNCStatus GNCStatusFromCppStatus(Status status) {
std::string endpoint_id = [endpointID cStringUsingEncoding:[NSString defaultCStringEncoding]];
PayloadListener listener;
listener.payload_cb = [&delegate](absl::string_view endpoint_id, Payload payload) {
listener.payload_cb = [delegate](absl::string_view endpoint_id, Payload payload) {
NSString *endpointID = @(std::string(endpoint_id).c_str());
GNCPayload *gncPayload = [GNCPayload fromCpp:std::move(payload)];
[delegate receivedPayload:gncPayload fromEndpoint:endpointID];
};
listener.payload_progress_cb =
[&delegate](absl::string_view endpoint_id, const PayloadProgressInfo &info) {
[delegate](absl::string_view endpoint_id, const PayloadProgressInfo &info) {
NSString *endpointID = @(std::string(endpoint_id).c_str());
GNCPayloadStatus status;
switch (info.status) {
@@ -57,7 +57,7 @@
}
#if __LP64__
// This cast is only safe in a 64-bit runtime.
blockResult = (NSData *)content;
blockResult = [(NSData *)content copy];
#else
blockResult = nil;
#endif