A couple NSRunLoop questions
A couple NSRunLoop questions
- Subject: A couple NSRunLoop questions
- From: Miles <email@hidden>
- Date: Mon, 4 May 2009 18:54:15 -0700
1) I have a timer on a run loop. Every so often I invalidate the timer which
as I understand it should remove the run loop as well. At a later time I
start a new run loop (by calling the same method as before), then invalidate
that timer after a while, and repeat.
When I pause the debugger after this happens a few times I notice that
sometimes the run loop threads have been successfully removed and sometimes
not. I should only ever see one at most, but sometimes I see two or more.
The timer in the extra threads hanging around are not running because it
would be obvious in my app if that was happening. I have an autorelease pool
around the code that creates and starts the run loop but I’m not sure it’s
doing anything.
My question is, what might be keeping the extra run loop threads around on
occasion?
Here's the important code:
[NSThread detachNewThreadSelector:@selector(startTileAnimation:)
toTarget:animationHelper withObject:nil];
-(void)startTileAnimation:(id)sender {
NSLog(@"start loop");
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
myRunLoop = [NSRunLoop currentRunLoop];
animationTimer = [NSTimer
scheduledTimerWithTimeInterval:ANIMATION_STEP_FREQUENCY target:self
selector:@selector(doTileAnimation:) userInfo:nil repeats:YES];
[myRunLoop addTimer:animationTimer forMode:NSDefaultRunLoopMode];
[myRunLoop run];
[autoreleasepool release];
}
-(void)stopTileAnimation {
if(animationTimer != nil) {
NSLog(@"stop loop");
[animationTimer invalidate];
animationTimer = nil;
}
}
2) My second question is, from within that run loop, is it safe to make a
copy of an array that’s in the main thread like this?
NSMutableArray *tmp = nil;
NSLock *theLock = [[NSLock alloc] init];
if( [theLock tryLock] ) {
tmp = [NSArray arrayWithArray:animatingTilesArray];
[theLock unlock];
}
On occasion I have gotten an error on the line where it tries to make a copy
of the array. animatingTilesArray and tmp are both of type NSMutableArray
The errors were things like ‘attempt to insert nil object at objects[0]'
(but when I looked at animatingTilesArray it had several non-nil objects in
it. I think I saw another error there but I can’t remember now what it was.
Thanks!
_______________________________________________
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