views:

199

answers:

1

Hi!

I have a piece of network code that uses AsyncSocket but moves it to a separate runloop. I'm creating this runloop with the following piece of code:

[NSThread detachNewThreadSelector:@selector(_workerLoop) toTarget:self withObject:nil];

and here's how my _workerLoop looks like (they're both in the same class):

-(void)_workerLoop {
    workerLoop = [[NSRunLoop currentRunLoop] retain];

    while(keepWorkerLoopRunning) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        [workerLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.5f]];
        [pool release];
    }

    [workerLoop release];
    workerLoop = nil;
}

Now, according to the docs, the NSThread will retain the target, and as this thread will only terminate when AsyncSocket disconnects, it's impossible to release and deallocate this object until the socket disconnects.

How can I fix this or maybe I'm doing something wrong?

A: 

I've solved this by refactoring the runloop constructor out into own class, referenced by parent class that handles the networking code. This way, parent object is being deallocated, it can stop the thread and release the runloop

esad