Calling CFRelease for a CFHostRef will sometimes crash - objective-c

This is a secondary question that arose out of a post I made earlier today. I have the method below, which works fine for what I need, but sometimes crashes when I call CFRelease on the hostRef variable. I think it may have to do with the resource being used elsewhere when I'm trying to release it, but as far as I can tell, I'm synchronously resolving the host and I'm not accessing it from another thread.
I tried calling CFHostCancelInfoResolution before CFRelease, but that didn't change the frequency of the crashes. I thought I would post this here to see if there are some assumptions or misconceptions I have that aren't true.
+ (NSArray *) addressesForHostname: (NSString *)hostname {
CFMutableArrayRef ipAddresses = nil;
DLog(#"Getting addresses for host name %#", hostname);
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)(hostname));
CFStreamError error;
BOOL didResolve = CFHostStartInfoResolution(hostRef, kCFHostAddresses, &error); // synchronously get the host.
if (didResolve) {
CFArrayRef responseObjects = CFHostGetAddressing(hostRef, NULL);
long numberOfResponses = CFArrayGetCount(responseObjects);
ipAddresses = CFArrayCreateMutable(kCFAllocatorDefault, numberOfResponses, &kCFTypeArrayCallBacks);
for ( int i = 0 ; i < numberOfResponses; ++i ) {
char * ipAddress = NULL;
CFDataRef responseObject = CFArrayGetValueAtIndex(responseObjects, i);
struct sockaddr * currentAddress = (struct sockaddr *) CFDataGetBytePtr(responseObject); // Unwrap the CFData wrapper aound the sockaddr struct
switch (currentAddress->sa_family) {
case AF_INET: { // Internetworking AKA IPV4
DLog(#"Extracting IPV4 address");
struct sockaddr_in * socketAddress = (struct sockaddr_in *) currentAddress;
ipAddress = malloc(sizeof(INET_ADDRSTRLEN));
inet_ntop(AF_INET,
&(socketAddress->sin_addr),
ipAddress,
INET_ADDRSTRLEN);
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
CFArrayInsertValueAtIndex(ipAddresses, i, ipAddressString);
break;
}
case AF_INET6: { // IPV6
DLog(#"Extracting IPV6 address");
struct sockaddr_in6 * socketAddress = (struct sockaddr_in6 *) currentAddress;
ipAddress = malloc(sizeof(INET6_ADDRSTRLEN));
inet_ntop(AF_INET6,
&(socketAddress->sin6_addr),
ipAddress,
INET6_ADDRSTRLEN);
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
CFArrayInsertValueAtIndex(ipAddresses, i, ipAddressString);
break;
}
default:
DLog(#"Unsupported addressing protocol encountered. Gracefully ignoring and continuing.");
break;
}
if(ipAddress != NULL) {
free(ipAddress);
}
}
CFRelease(responseObjects);
}
CFRelease(hostRef);
return (__bridge_transfer NSArray *) ipAddresses;
}

removing CFRelease(responseObjects); shall fix it

You must follow the Create Rule for Core Foundation objects. If you received the object by calling a function with the words Create or Copy in their names (or if you call CFRetain explicitly), then you must release (CFRelease) the object when you're done with it. If you did not receive the object this way, then you must not release the object.
There are several mistakes in your code. First, the one you're finding, which is around responseObjects. You fetch this object using:
CFDataRef responseObject = CFArrayGetValueAtIndex(responseObjects, i);
The function does not have Create or Copy in its name. You must not call CFRelease on it.
However, you also call this:
ipAddresses = CFArrayCreateMutable(kCFAllocatorDefault, numberOfResponses, &kCFTypeArrayCallBacks);
and in a couple of places:
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
You must call CFRelease on these objects before they go out of scope or you will leak them.

I think CFStreamError error; may cause this issue.
Try to declare error to null?
// synchronously get the host.
BOOL didResolve = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);

Related

How to store blocks in Objective C?

I started writing a simple JSON RPC TCP library in Objective C.
I have a method that invokes a RPC Method:
- (void)invokeMethod:(NSString *)method
withParameters:(id)parameters
requestId:(id)requestId
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
NSAssert(NSClassFromString(#"NSJSONSerialization"), #"NSJSONSerialization not found!");
NSDictionary *requestObject = #{#"jsonrpc": #"2.0",
#"method": method,
#"params": parameters,
#"id": requestId};
NSError *error = nil;
NSData *jsondData = [NSJSONSerialization dataWithJSONObject:requestObject options:0 error:&error];
if (error){
return failure(error);
}
[self->callbacks setObject:#{#"success": success ? [success copy] : [NSNull null],
#"failure": failure ? [failure copy] : [NSNull null]}
forKey:requestId];
NSString *str = [[NSString alloc] initWithData:jsondData encoding:NSUTF8StringEncoding];
NSLog(#"Sending: %#", str);
[self.socket writeData:jsondData withTimeout:-1 tag:1];
}
The class basically represents a TCP connection, when calling the above method, the JSON data is sent with an id over TCP to the server which either returns a success or a failure:
- (void) socket:(GCDAsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag
{
NSError *error = nil;
[self.socket readDataWithTimeout:-1 tag:2];
// … rpc response parsing code here, removed for simplicity …
// detect if error or success
NSDictionary *cbs = [self->callbacks objectForKey:JSONRPCObjectId];
void(^success)(id resultObject) = [cbs objectForKey:#"success"];
success ? success(JSONRPCObjectResult) : nil;
return;
}
Now, I am unsure how to keep track of the success and failure blocks, currently I am storing them in an NSMutableDict, using the requestId as key. Is it fine to do this or is there a better approach that I should use?
Blocks in objective-c are objects and you can treat the same way as other object, so storing them in NSDictionarys, NSArrays etc is perfectly fine. The only catch is that blocks when initially created exist in the same memory scope as local variable do and so they are no longer valid when the method that the block is defined in returns, just like all other local variables so you have to copy them first, just copy them and put the copy in the collection. There is a block copy function but you can just send them a copy message [myBlock copy];
Quick answer, seeing as you don't have anything workable yet...
This is more than you asked for; so, you'll probably have to pair it down to meet your specific need. Basically, it stores as many blocks as you specify at contiguous memory addresses. Paste this into a header file or somewhere global to the method from which you will call these:
typedef const typeof(id(^)(void)) retained_object;
static id (^retainable_object)(id(^)(void)) = ^ id (id(^object)(void)) {
return ^{
return object();
};
};
typeof (retained_object) *(^(^retain_object)(id (^__strong)(void)))(void) = ^ (id(^retainable_object)(void)) {
typeof(retained_object) * object_address;
object_address = &retainable_object;
typeof(retained_object) * persistent_object = (typeof(retained_object) *)CFBridgingRetain(retainable_object);
return ^ typeof(retained_object) * {
return persistent_object;
};
};
static void (^(^iterator)(const unsigned long))(id(^)(void)) = ^ (const unsigned long object_count) {
id const * retained_objects_ref[object_count];
return ^ (id const * retained_objects_t[]) {
return ^ (id(^object)(void)) {
object();
int index = 0UL;
int * index_t = &index;
for (; (*index_t) < object_count; ((*index_t) = (*index_t) + 1UL)) printf("retained_object: %p\n", (*((id * const)retained_objects_t + (object_count - index)) = retain_object(retainable_object(object()))));
};
}(retained_objects_ref);
};
From some method, add:
iterator(1000)(^ id { return (^{ printf("stored block\n"); }); });
This should store 1,000 blocks at as many unique memory addresses.

how to resolve hostname from ip address in iOS Objective-C

I'm looking for a way to resolve the hostname of a device in my LAN from its ip address on this LAN.
I wrote a program in C which works perfectly on Linux using gethostbyaddr() function.
When I tried that on OS X or iOS it doesn't work.
It seems that there is a problem with gethostbyaddr() in OS X and iOS.
Anyway, if you have another idea to get hostname of remote machine from it's IP in iOS, it'll make my day.
This is the code I used:
First test:
192.168.0.101 is the ip address of the machine that we are querying for hostname.
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
struct hostent *he;
struct in_addr ipv4addr;
inet_pton(AF_INET, "192.168.0.101", &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
printf("Host name: %s\n", he->h_name);
This code works well on linux, but it doesn't on OS X nor iOS.
Second test:
+ (NSArray *)hostnamesForAddress:(NSString *)address {
// Get the host reference for the given address.
struct addrinfo hints;
struct addrinfo *result = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
int errorStatus = getaddrinfo([address cStringUsingEncoding:NSASCIIStringEncoding], NULL, &hints, &result);
if (errorStatus != 0) return nil;
CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
if (addressRef == nil) return nil;
freeaddrinfo(result);
CFHostRef hostRef = CFHostCreateWithAddress(kCFAllocatorDefault, addressRef);
if (hostRef == nil) return nil;
CFRelease(addressRef);
BOOL isSuccess = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL);
if (!isSuccess) return nil;
// Get the hostnames for the host reference.
CFArrayRef hostnamesRef = CFHostGetNames(hostRef, NULL);
NSMutableArray *hostnames = [NSMutableArray array];
for (int currentIndex = 0; currentIndex < [(NSArray *)hostnamesRef count]; currentIndex++) {
[hostnames addObject:[(NSArray *)hostnamesRef objectAtIndex:currentIndex]];
}
return hostnames;
}
This code stucks at CFHostStartInfoResolution returning nil at this point.
Thx in advance.
It's actually a one-liner.
[[NSHost hostWithAddress:#"173.194.34.24"] name]

How to get read/write streams after dns_sd DNSServiceResolve in iOS

The goal is to get read/write streams after a service is successfully resolved by with the dns_sd API. I started with Apple's DNSSDObjects sample project and I'm editing the DNSSDService.m file to get read and write streams after the service is resolved.
Here's what I've got so far. It seems like it should work, but it does't :(
I got this far by following the code on this thread, though I'm not entirely sure that it's how this should be done.
EDIT: Apple's documentation here confirms that this is how it should be done..."So, once you've resolved the service using DNSServiceResolve, you should pass the service's DNS name (the hosttarget parameter to your DNSServiceResolveReply callback) to a connect-by-name API (like CFStreamCreatePairWithSocketToHost)."
// Called by DNS-SD when something happens with the resolve operation.
static void ResolveReplyCallback(
DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char * fullname,
const char * hosttarget,
uint16_t port,
uint16_t txtLen,
const unsigned char * txtRecord,
void * context
)
{
CFStringRef host = CFStringCreateWithCString(kCFAllocatorDefault,
hosttarget,
kCFStringEncodingUTF8);
DNSSDService * obj;
#pragma unused(interfaceIndex)
assert([NSThread isMainThread]); // b/c sdRef dispatches to the main queue
obj = (__bridge DNSSDService *) context;
assert([obj isKindOfClass:[DNSSDService class]]);
assert(sdRef == obj->sdRef_);
#pragma unused(sdRef)
#pragma unused(flags)
#pragma unused(fullname)
#pragma unused(txtLen)
#pragma unused(txtRecord)
if (errorCode == kDNSServiceErr_NoError) {
[obj resolveReplyWithTarget:[NSString stringWithUTF8String:hosttarget]
port:ntohs(port)];
} else {
[obj stopWithError:[NSError errorWithDomain:NSNetServicesErrorDomain
code:errorCode
userInfo:nil]
notify:YES];
}
//now let's get read&write streams?
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
host,
port,
&readStream,
&writeStream
);
if (readStream && writeStream) {
CFReadStreamSetProperty(readStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue);
obj.inputStream = (__bridge_transfer NSInputStream *) readStream;
obj.outputStream = (__bridge_transfer NSOutputStream *) writeStream;
}
CFRelease(host);
}

iOS - How do I create a read and write stream to a network socket?

I am new to iOS development. I am attempting to create a read and write stream. I am using the CFNetworking programming guide's examples to try and get something working.
I am trying to schedule the read stream on the run loop to work around the issue of the streams blocking. Right away I have run into issues. How can I create a CFHost object using CFHhostCreateWithAddress? Here is what I have so far:
NSString *address = #"irc.ubuntu.net";
CFDataRef addressDataRef = (CFDataRef)[address dataUsingEncoding:NSASCIIStringEncoding];
CFHostRef host = CFHostCreateWithAddress(kCFAllocatorDefault, addressDataRef);
//Create Read and Write Stream
CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, host, 8008, &readStream, &writeStream);
The second line bombs. Can someone please tell me how to create a CFHostRef?
Thanks a lot!
The documentation states that the second argument to CFHostCreateWithAddress() must be "A CFDataRef object containing a sockaddr structure for the address of the host. This value must not be NULL."
You're passing a CFDataRef representing "irc.ubuntu.net", which is by no means a sockaddr struct.
Use CFHostCreateWithName:
CFHostRef CFHostCreateWithName (
CFAllocatorRef allocator,
CFStringRef hostname
);
As you probably know, you can cast an NSString * to CFStringRef, or create a constant CFStringRef with the macro CFSTR().
+ (NSData *)dataForIPAddress:(NSString *)address {
struct addrinfo hints;
struct addrinfo *result = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
int errorStatus = getaddrinfo([address cStringUsingEncoding:NSASCIIStringEncoding], NULL, &hints, &result);
if (errorStatus != 0) return nil;
CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
if (addressRef == nil) return nil;
freeaddrinfo(result);
return [(NSData *)addressRef autorelease];
}

How to use CFNetwork to get byte array from sockets?

I'm working in a project for the iPad, it is a small program and I need it to communicate with another software that runs on windows and act like a server; so the application that I'm creating for the iPad will be the client.
I'm using CFNetwork to do sockets communication, this is the way I'm establishing the connection:
char ip[] = "192.168.0.244";
NSString *ipAddress = [[NSString alloc] initWithCString:ip];
/* Build our socket context; this ties an instance of self to the socket */
CFSocketContext CTX = { 0, self, NULL, NULL, NULL };
/* Create the server socket as a TCP IPv4 socket and set a callback */
/* for calls to the socket's lower-level connect() function */
TCPClient = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketDataCallBack, (CFSocketCallBack)ConnectCallBack, &CTX);
if (TCPClient == NULL)
return;
/* Set the port and address we want to listen on */
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr([ipAddress UTF8String]);
CFDataRef connectAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));
CFSocketConnectToAddress(TCPClient, connectAddr, -1);
CFRunLoopSourceRef sourceRef = CFSocketCreateRunLoopSource(kCFAllocatorDefault, TCPClient, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), sourceRef, kCFRunLoopCommonModes);
CFRelease(sourceRef);
CFRunLoopRun();
And this is the way I sent the data, which basically is a byte array
/* The native socket, used for various operations */
// TCPClient is a CFSocketRef member variable
CFSocketNativeHandle sock = CFSocketGetNative(TCPClient);
Byte byteData[3];
byteData[0] = 0;
byteData[1] = 4;
byteData[2] = 0;
send(sock, byteData, strlen(byteData)+1, 0);
Finally, as you may have noticed, when I create the server socket, I registered a callback for the kCFSocketDataCallBack type, this is the code.
void ConnectCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
// SocketsViewController is the class that contains all the methods
SocketsViewController *obj = (SocketsViewController*)info;
UInt8 *unsignedData = (UInt8 *) CFDataGetBytePtr(data);
char *value = (char*)unsignedData;
NSString *text = [[NSString alloc]initWithCString:value length:strlen(value)];
[obj writeToTextView:text];
[text release];
}
Actually, this callback is being invoked in my code, the problem is that I don't know how can I get the data that the windows client sent me, I'm expecting to receive an array of bytes, but I don't know how can I get those bytes from the data param.
If anyone can help me to find a way to do this, or maybe me point me to another way to get the data from the server in my client application I would really appreciate it.
Thanks.
In your callback, the data parameter is indeed a CFDataRef value for the kCFSocketDataCallBack callback type.
CFDataRef dataRef = (CFDataRef) data;
Byte *array = new Byte[CFDataGetLength(dataRef)]; // Or use a fixed length
CFDataGetBytes(dataRef, CFRangeMake(0, CFDataGetLength(theData)), array);
The array will then contains the array of byte.