I am using an instance of the this class to pass necessary values to a function for sending Socket Data:
@interface SsdpParameters : NSObject
{
CFDataRef msg;
CFDataRef addr;
CFSocketRef sock;
}
@property CFDataRef msg;
@property CFDataRef addr;
@property CFSocketRef sock;
@end
This is the function that is responsible for sending the socket data:
-(void)SendSsdpResponse:(id)parameters
{
SsdpParameters *params = parameters;
CFDataRef msg = (CFDataRef)params.msg;
CFDataRef addr = (CFDataRef)params.addr;
CFSocketRef sock = (CFSocketRef)params.sock;
CFSocketError err = CFSocketSendData (sock, addr, msg, 0); // Program received signal: "EXC_BAD_ACCESS".
if (err)
{
NSLog(@"Error sending Valid Response");
}
}
This function sets up the socket and calls SendSsdpResponse after a 1 second delay:
- (void) sendValidResponses
{
NSMutableString *message = nil;
CFSocketRef sock = [self newSSDPSendSocket];
if(sock != nil)
{
struct sockaddr_in SSDPaddr;
memset(&SSDPaddr, 0, sizeof(SSDPaddr));
SSDPaddr.sin_family=AF_INET;
SSDPaddr.sin_addr.s_addr=inet_addr(SSDP_ADDRESS);
SSDPaddr.sin_port=htons(SSDP_PORT);
// Loop through list, sending SSDP
for (NSString *aKey in list)
{
message = [[NSMutableString alloc] initWithString:@"NOTIFY * HTTP/1.1\r\n"];
// Append more lines to message.
CFDataRef addr = CFDataCreate(NULL, (const UInt8*)&SSDPaddr, sizeof(SSDPaddr));
CFDataRef msg = CFDataCreate(NULL, (const UInt8*)[message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
SsdpParameters *parameters = [[SsdpParameters alloc] init];
parameters.msg = msg;
parameters.addr = addr;
parameters.sock = sock;
[self performSelector:@selector(SendSsdpResponse:) withObject:parameters afterDelay:1.0];
CFRelease(addr);
CFRelease(msg);
[message release];
}
CFRelease(sock);
}
}
As you can see by the comment, in SendSsdpResponse, I an getting a “EXC_BAD_ACCESS” error when trying to call CFSendSocketData. I have a suspicion that it’s because I am “passing” sock, addr, and msg and there is something the runtime does not like about this. I have run into this problem before with passing variables of the CF data types to other functions. I have yet to find an answer and hope someone here can finally help me understand what’s going on behind the scenes.
Thanks!
EXC_BAD_ACCESSerrors generally indicate that you’re attempting to invoke a method on an instance that has already been deallocated. This is often the case when you fail to properlyretainvariables, especially when they’re being passed between functions.When you perform the selector with the delay, on this line:
You’re providing the method with the
parametersvariable, which has the properties as defined in the header you provided. The problem is that right after theperformSelector:method, you’re releasing the memory referred toaddrandmsg. Since you’re performing this selector with a delay, the memory that addr and msg point to is being deallocated prior to its use.So, once this memory has been released, it is no longer “good” in the
SsdpParametersobject. You should (in the setter for these properties, on theSsdpParametersobject) copy this memory, so that the original callers can release it safely.You should investigate this function:
Then, your setter could look like: