Trouble with performSelector afterDelay
Trouble with performSelector afterDelay
- Subject: Trouble with performSelector afterDelay
- From: "James Trankelson" <email@hidden>
- Date: Mon, 14 Jul 2008 14:53:37 -0400
Hi,
I've been experiencing some strangeness that I can't seem to
understand surrounding my use of performSelector afterDelay.
The situation is as follows. I have a class, 'Listener', that, when
initialized, spawns a thread to listen on a port:
class Listener:
- (id) init
{
self = [super init];
[NSThread detachNewThreadSelector:@selector(startAsyncListener:)
toTarget:self withObject:nil];
return self;
}
- (void)startAsyncListener:(id)arg
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
aSyncUpdatePort = (NSMachPort *)[NSMachPort port];
[aSyncUpdatePort setDelegate:self];
[[NSRunLoop currentRunLoop] addPort:aSyncUpdatePort
forMode:NSDefaultRunLoopMode];
for(;;) [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]]; <===*
[pool release];
}
Now, on my main thread, I first allocate (and initialize) an instance
of Listener, and then spawn another thread to do some work in another
class which I'll call "Worker". Sometimes during the execution of this
other thread, I decide I want to perform a selector after some delay,
and the way I do this is to package up a little message that I send to
the port on my Listener class that basically says "you should do a
performSelector afterDelay". So, my Listener class gets this message,
calls performSelector afterDelay and before the selector is invoked,
my Worker class gets its dealloc method called and everything
explodes. My Listener class handles messages all day long, but when it
tries to call performSelector afterDelay, it causes the Worker to
deallocate itself. I have no idea why.
If I set a breakpoint in the dealloc of my Worker class, I see the
following backtrace when this happens:
#0 0x0002606d in -[Worker release] at Worker.m:155
#1 0x94e8502a in __delayedPerformCleanup
#2 0x9390d92b in CFRunLoopTimerInvalidate
#3 0x9390e879 in CFRunLoopRunSpecific
#4 0x9390ecf8 in CFRunLoopRunInMode
#5 0x94e734a5 in -[NSRunLoop(NSRunLoop) runMode:beforeDate:]
#6 0x000fea26 in -[Listener startAsyncListener:] at Listener.m:141
#7 0x94e3ef1d in -[NSThread main]
#8 0x94e3eac4 in __NSThread__main__
#9 0x96bfb6f5 in _pthread_start
#10 0x96bfb5b2 in thread_start
Where, Worker.m:155 is the line in startAsyncListener marked (<===) above.
Does anyone have any ideas what I might be doing wrong?
Thanks.
-jt
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden