Search code examples
objective-ciosmemory-managementuiviewcontrollerdealloc

What things should I look for that could be increasing my retain count in a UIViewController which doesn't dealloc


I have a UIViewController that is popped from the navigation stack on iPhone and removed by setRootViewController on the iPad.

In both cases the UIViewController fails to dealloc, which means that something is hanging on to it; this is confirmed by logging [self retainCount] which is two, right before the pop or the setRootViewController (for some reason it's four in ViewWillDisappear).

The UIView has audio (using Flite Text to Speech - which uses AVFoundation.framework) and animation.

What things should I look for that could increasing my retain count in the view controller and stopping the view controller from being politely dealloced as it should be.

Here's how the view is pushed onto the view stack or set as the RootViewController;

-(IBAction)pushShare:(id)sender{

    ShareViewController *shareViewController =  [[ShareViewController alloc] initWithNibName:@"ShareViewController" bundle:nil];
    NSLog(@"1. SVC Retain count %d", [shareViewController retainCount]);
    [shareViewController setParentIsIpadMake:YES];
    NSLog(@"2. SVC Retain count %d", [shareViewController retainCount]);    
    [shareViewController setStory:story];
    NSLog(@"3. SVC Retain count %d", [shareViewController retainCount]);      
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {      
        StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate.window setRootViewController:shareViewController];
        NSLog(@"4. SVC Retain count %d", [shareViewController retainCount]);      
    } else{


        [self.navigationController pushViewController:shareViewController animated:YES];
        NSLog(@"4. SVC Retain count %d", [shareViewController retainCount]);      

    }

    NSLog(@"releasing Svc...");
    [shareViewController release];
    NSLog(@"5. SVC Retain count %d", [shareViewController retainCount]);

}

Solution

  • I read this question about NSTimer stopping the dealloc of a UIView. Turns out I had a NSTimer firing my animations which was stopping dealloc from happening nicely. I fixed it by adding a method called killTimer:

    -(void)killTimer{
        [animationTimer invalidate];
        animationTimer = nil;
    }
    

    which I called on closing the view.

    Thanks heaps for the answers to this question though - they were super helpful. I didn't actually know about the static analyzer until asking - or about how useless retain counts are; so +1 to you both.