Hi all,
I'm new to the world of objective-c and cocoa programming, and I'm finding it a bit disorienting right now. So when I wanted to write an app that required wireless communication over wifi (done with Bonjour), I tried my best to find chunks of code that I could reuse.
I'm using AsyncSockets to handle the data transfer details after bonjour introduces my Mac app to the iPhone.
I get an EXC_BAD_ACCESS error in my app, which I've read has to do with memory management. However, my best attempts to find the source of the error (even just WHERE it crashes) have only lead me to think that the error is in AsyncSocket.m. This is unlikely because that code worked for me elsewhere (unless a difference between MacOS and the iOS is causing this). Here is the function:
The bold line is where the debugger breaks on error. In the debugger window, I see peeraddr's value as 0x0 and summary shows "<invalid CFDataRef>".
I'm reluctant to think that the error comes from this code, because it worked for me before, and it's used by a lot of people. I must be doing something wrong elsewhere but I can't for the life of me figure it out. Any help to help track this down is greatly appreciated.
Sorry if this post is a bit incoherent. I'm writing this late at night after hours of struggling with one annoying bug... we all know what that feels like
I'm new to the world of objective-c and cocoa programming, and I'm finding it a bit disorienting right now. So when I wanted to write an app that required wireless communication over wifi (done with Bonjour), I tried my best to find chunks of code that I could reuse.
I'm using AsyncSockets to handle the data transfer details after bonjour introduces my Mac app to the iPhone.
I get an EXC_BAD_ACCESS error in my app, which I've read has to do with memory management. However, my best attempts to find the source of the error (even just WHERE it crashes) have only lead me to think that the error is in AsyncSocket.m. This is unlikely because that code worked for me elsewhere (unless a difference between MacOS and the iOS is causing this). Here is the function:
Code:
- (BOOL)setSocketFromStreamsAndReturnError:(NSError **)errPtr
{
// Get the CFSocketNativeHandle from theReadStream
CFSocketNativeHandle native;
CFDataRef nativeProp = CFReadStreamCopyProperty(theReadStream, kCFStreamPropertySocketNativeHandle);
if(nativeProp == NULL)
{
if (errPtr) *errPtr = [self getStreamError];
return NO;
}
CFDataGetBytes(nativeProp, CFRangeMake(0, CFDataGetLength(nativeProp)), (UInt8 *)&native);
CFRelease(nativeProp);
CFSocketRef socket = CFSocketCreateWithNative(kCFAllocatorDefault, native, 0, NULL, NULL);
if(socket == NULL)
{
if (errPtr) *errPtr = [self getSocketError];
return NO;
}
// Determine whether the connection was IPv4 or IPv6
CFDataRef peeraddr = CFSocketCopyPeerAddress(socket);
[B]struct sockaddr *sa = (struct sockaddr *)CFDataGetBytePtr(peeraddr);[/B]
if(sa->sa_family == AF_INET)
{
theSocket = socket;
}
else
{
theSocket6 = socket;
}
return YES;
}
I'm reluctant to think that the error comes from this code, because it worked for me before, and it's used by a lot of people. I must be doing something wrong elsewhere but I can't for the life of me figure it out. Any help to help track this down is greatly appreciated.
Sorry if this post is a bit incoherent. I'm writing this late at night after hours of struggling with one annoying bug... we all know what that feels like