Skip to content

Commit

Permalink
Fix EXC_BAD_ACCESS crash when calling payload delegate methods.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bourdakos1 authored and Copybara-Service committed May 22, 2023
1 parent c9884f6 commit 3c72e4f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@ - (void)acceptConnectionRequestFromEndpoint:(NSString *)endpointID
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ - (NSData *)readMaxLength:(NSUInteger)length error:(NSError **)error {
}
#if __LP64__
// This cast is only safe in a 64-bit runtime.
blockResult = (NSData *)content;
blockResult = [(NSData *)content copy];
#else
blockResult = nil;
#endif
Expand Down

0 comments on commit 3c72e4f

Please sign in to comment.