Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

99miles

macrumors member
Original poster
Oct 10, 2008
50
0
I'm starting a new NSTimer on a run loop, and the thread seems to sometimes stick around even when I think I got rid of it. I can't seem to figure out how to get rid of it.

I call:

Code:
[NSThread detachNewThreadSelector:@selector(startTileAnimation:) toTarget:animationHelper withObject:nil];

... which calls a method which says:

Code:
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
animationTimer	= [NSTimer scheduledTimerWithTimeInterval:ANIMATION_STEP_FREQUENCY target:self selector:@selector(doTileAnimation:) userInfo:nil repeats:YES];
[myRunLoop addTimer:animationTimer forMode:NSDefaultRunLoopMode];
[autoreleasepool release];

... and when I want to stop it, I call:

Code:
-(void)stopAnimation {
	if(animationTimer != nil) {
		[animationTimer invalidate];
		animationTimer = nil;
	}
}

I start and stop this every so often, but I never have two going at once -- at least I am not meaning to.
I put a log in the methods that stop and start the new threads and they both get called when i expect. But in the XCode debugger I sometimes see that even when i cancel a timer the thread sticks around. I thought that by invalidating the timer on that run loop the run loop would go away since there's nothing else on that run loop. But, that isn't always the case. Is there a way to remove a detached thread completely, that I had started earlier?

Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.