How to solve a deallocated connection in iPhone SDK 3.1.3? - Streams - CFSockets
Posted
by Christian
on Stack Overflow
See other posts from Stack Overflow
or by Christian
Published on 2010-03-18T14:25:12Z
Indexed on
2010/03/18
23:01 UTC
Read the original article
Hit count: 486
Hi everyone,
Debugging my implementation I found a memory leak issue. I know where is the issue, I tried to solve it but sadly without success. I will try to explain you, maybe someone of you can help with this.
First I have two classes involved in the issue, the publish class (where publishing the service and socket configuration is done) and the connection (where the socket binding and the streams configuration is done). The main issue is in the connection via native socket. In the 'publish' class the "server" accepts a connection with a callback. The callback has the native-socket information. Then, a connection with native-socket information is created. Next, the socket binding and the streams configuration is done. When those actions are successful the instance of the connection is saved in a mutable array. Thus, the connection is established.
static void AcceptCallback(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
Publish *rePoint = (Publish *)info;
if ( type != kCFSocketAcceptCallBack) {
return;
}
CFSocketNativeHandle nativeSocketHandle = *((CFSocketNativeHandle *)data);
NSLog(@"The AcceptCallback was called, a connection request arrived to the server");
[rePoint handleNewNativeSocket:nativeSocketHandle];
}
- (void)handleNewNativeSocket:(CFSocketNativeHandle)nativeSocketHandle{
Connection *connection = [[[Connection alloc] initWithNativeSocketHandle:nativeSocketHandle] autorelease]; // Create the connection
if (connection == nil) {
close(nativeSocketHandle);
return;
}
NSLog(@"The connection from the server was created now try to connect");
if ( ! [connection connect]) {
[connection close];
return;
}
[clients addObject:connection]; //save the connection trying to avoid the deallocation
}
The next step is receive the information from the client, thus a read-stream callback is triggered with the information of the established connection. But when the callback-handler tries to use this connection the error occurs, it says that such connection is deallocated. The issue here is that I don't know where/when the connection is deallocated and how to know it. I am using the debugger, but after some trials, I don't see more info.
void myReadStreamCallBack (CFReadStreamRef stream, CFStreamEventType eventType, void *info) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Connection *handlerEv = [[(Connection *)info retain] autorelease]; // The error -[Connection retain]: message sent to deallocated instance 0x1f5ef0 (Where 0x1f5ef0 is the reference to the established connection)
[handlerEv readStreamHandleEvent:stream andEvent:eventType];
[pool drain];
}
void myWriteStreamCallBack (CFWriteStreamRef stream, CFStreamEventType eventType, void *info){
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
Connection *handlerEv = [[(Connection *)info retain] autorelease]; //Sometimes the error also happens here, I tried without the pool, but it doesn't help neither.
[handlerEv writeStreamHandleEvent:eventType];
[p drain];
}
Something strange is that when I run the debugger(with breakpoints) everything goes well, the connection is not deallocated and the callbacks work fine and the server is able to receive the message. I will appreciate any hint!
© Stack Overflow or respective owner