From 23b5d0bf753066007bf9de7608046eeaa424ea0a Mon Sep 17 00:00:00 2001 From: Ashitha Santhosh Date: Thu, 10 Sep 2020 18:47:51 -0700 Subject: [PATCH] Fix memory leak. --- src/core/lib/iomgr/endpoint_cfstream.cc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/core/lib/iomgr/endpoint_cfstream.cc b/src/core/lib/iomgr/endpoint_cfstream.cc index 1751b4fcd43..75cd0f09d1a 100644 --- a/src/core/lib/iomgr/endpoint_cfstream.cc +++ b/src/core/lib/iomgr/endpoint_cfstream.cc @@ -365,13 +365,17 @@ grpc_endpoint* grpc_cfstream_endpoint_create( CFSTREAM_HANDLE_REF(ep_impl->stream_sync, "endpoint create"); ep_impl->peer_string = peer_string; - const int* native_handle = - reinterpret_cast(CFReadStreamCopyProperty( - ep_impl->read_stream, kCFStreamPropertySocketNativeHandle)); grpc_resolved_address resolved_local_addr; resolved_local_addr.len = sizeof(resolved_local_addr.addr); - if (getsockname(*native_handle, - reinterpret_cast(resolved_local_addr.addr), + CFDataRef native_handle = static_cast(CFReadStreamCopyProperty( + ep_impl->read_stream, kCFStreamPropertySocketNativeHandle)); + CFSocketNativeHandle sockfd; + CFDataGetBytes(native_handle, CFRangeMake(0, sizeof(CFSocketNativeHandle)), + (UInt8*)&sockfd); + if (native_handle) { + CFRelease(native_handle); + } + if (getsockname(sockfd, reinterpret_cast(resolved_local_addr.addr), &resolved_local_addr.len) < 0) { ep_impl->local_address = ""; } else {