开发者

How to solve a deallocated connection in iPhone SDK 3.1.3? - Streams - CFSockets

开发者 https://www.devze.com 2022-12-23 02:58 出处:网络
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.

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 an开发者_C百科d 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!


I remove the autorelease from this line:

Connection *handlerEv = [[(Connection *)info retain] autorelease];

Now, it seems to work, but I will try more things, because I am not sure when is released and if I will have "side-effects"

Please any hint is very helpful!

0

精彩评论

暂无评论...
验证码 换一张
取 消