Removing threads started from detachNewThreadSelector

Discussion in 'iOS Programming' started by 99miles, May 3, 2009.

  1. macrumors member

    Oct 10, 2008
    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:

    [NSThread detachNewThreadSelector:@selector(startTileAnimation:) toTarget:animationHelper withObject:nil];
    ... which calls a method which says:

    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:

    -(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?


Share This Page