Skip to content

Commit d321c5c

Browse files
committed
Bug fix for issue robbiehanson#146 - GCDAsyncSocket doesn't dealloc properly without disconnect call.
1 parent 4f68e8c commit d321c5c

File tree

4 files changed

+420
-297
lines changed

4 files changed

+420
-297
lines changed

GCD/GCDAsyncSocket.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#import <Security/SecureTransport.h>
1414
#import <dispatch/dispatch.h>
1515

16+
#include <sys/socket.h> // AF_INET, AF_INET6
17+
1618
@class GCDAsyncReadPacket;
1719
@class GCDAsyncWritePacket;
1820
@class GCDAsyncSocketPreBuffer;
@@ -930,13 +932,33 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
930932

931933
#pragma mark Utilities
932934

935+
/**
936+
* The address lookup utility used by the class.
937+
* This method is synchronous, so it's recommended you use it on a background thread/queue.
938+
*
939+
* The special strings "localhost" and "loopback" return the loopback address for IPv4 and IPv6.
940+
*
941+
* @returns
942+
* A mutable array with all IPv4 and IPv6 addresses returned by getaddrinfo.
943+
* The addresses are specifically for TCP connections.
944+
* You can filter the addresses, if needed, using the other utility methods provided by the class.
945+
**/
946+
+ (NSMutableArray *)lookupHost:(NSString *)host port:(uint16_t)port error:(NSError **)errPtr;
947+
933948
/**
934949
* Extracting host and port information from raw address data.
935950
**/
951+
936952
+ (NSString *)hostFromAddress:(NSData *)address;
937953
+ (uint16_t)portFromAddress:(NSData *)address;
954+
955+
+ (BOOL)isIPv4Address:(NSData *)address;
956+
+ (BOOL)isIPv6Address:(NSData *)address;
957+
938958
+ (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr fromAddress:(NSData *)address;
939959

960+
+ (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr family:(sa_family_t *)afPtr fromAddress:(NSData *)address;
961+
940962
/**
941963
* A few common line separators, for use with the readDataToData:... methods.
942964
**/
@@ -1058,7 +1080,22 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
10581080
* Called when a socket disconnects with or without error.
10591081
*
10601082
* If you call the disconnect method, and the socket wasn't already disconnected,
1061-
* this delegate method will be called before the disconnect method returns.
1083+
* then an invocation of this delegate method will be enqueued on the delegateQueue
1084+
* before the disconnect method returns.
1085+
*
1086+
* Note: If the GCDAsyncSocket instance is deallocated while it is still connected,
1087+
* and the delegate is not also deallocated, then this method will be invoked,
1088+
* but the sock parameter will be nil. (It must necessarily be nil since it is no longer available.)
1089+
* This is a generally rare, but is possible if one writes code like this:
1090+
*
1091+
* asyncSocket = nil; // I'm implicitly disconnecting the socket
1092+
*
1093+
* In this case it may preferrable to nil the delegate beforehand, like this:
1094+
*
1095+
* asyncSocket.delegate = nil; // Don't invoke my delegate method
1096+
* asyncSocket = nil; // I'm implicitly disconnecting the socket
1097+
*
1098+
* Of course, this depends on how your state machine is configured.
10621099
**/
10631100
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err;
10641101

0 commit comments

Comments
 (0)