I set the context to the address of the class that created the socket like this:
CFSocketContext ctxt = {0, (__bridge void *)(self), NULL, NULL, NULL};
self.sock = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketDataCallBack|kCFSocketConnectCallBack, *(CFSocketCallBack)takeCallback, &ctxt);
Then in the callback I put this:
static void takeCallback(CFSocketRef s, CFSocketCallBackType callbackType, CFDataRef address, const void *data, void *info)
{
PUFTransCont* obj = (__bridge PUFTransCont *)(info);
}
I saw Apple sample code that does this and saw samples all over the internet doing this. Why is it not working when I did it. Whenever the execution hits
PUFTransCont* obj = (__bridge PUFTransCont *)(info);
I get a EXC_BAD_ACCESS
Your code looks OK, but you have to ensure that the PUFTransCont
object is not deallocated
as long as it is used by the callback function.
One possible solution is to keep a strong reference to the object while the socket is in use (assuming that you compile with ARC).
Another possible solution is to transfer the ownership with
CFSocketContext ctxt = {0, CFBridgingRetain(self), NULL, NULL, NULL};
(which increases the retain count of the object), and release it later when the object is no longer needed, for example in the callback with
CFRelease(info);