0

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]);

}
glenstorey
  • 5,134
  • 5
  • 39
  • 71

3 Answers3

1

First, try running Xcode's static analyser, it may find what is wrong.

You can also try overriding retain method with calling [super retain] and logging. Moreover you can put a breakpoint there and look at the stack when Xcode stops on it.

Or you can run ARC conversion tool and see what happens.

Vadim Yelagin
  • 3,112
  • 2
  • 17
  • 20
  • Static Analyser didn't find anything (relevant); ARC conversation is something I'm not ready for yet - I'll try overriding retain and let you know how I go on. Thanks – glenstorey Mar 25 '12 at 07:46
1

Retain count method is next to useless. The system libraries will be retaining your VC and releasing it. Which is why sometimes it will be a strangely high number. There are lots of topics on this.

Don't be concerned about the view animation delegate having the VC. The view and the VC are intimately tied together and released together, and the view doesn't retain its animation delegate - it just assigns it as a reference.

If u are testing in the simulator, force a memory warning and see if it is dealloc'ed. Dealloc'ed isn't always called immediately when you pop the VC.

Run instruments leaks. Does it say the VC is leaking memory? Run instruments with Zombies to find out whe it is leaking.

bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
  • Thanks for your answer, it's really informative. 1. I wondered about the retain count, thanks for that. 2. Good to know about the animation delegate. 3. I forced a memory warning and it didn't dealloc. 4. I ran instruments leaks and it didn't detect any leaks, other than a AudioToolbox UISoundNewDevice 16 Byte Malloc, which I understand is a AVToolbox leak and not related to this problem. – glenstorey Mar 25 '12 at 08:36
0

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.

Community
  • 1
  • 1
glenstorey
  • 5,134
  • 5
  • 39
  • 71
  • Ok, so you had a retain cycle... there are other cases where you can get retain cycles. Read this: http://www.mikeash.com/pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html ... and subscribe to the blog. Very educational. – bandejapaisa Mar 27 '12 at 09:15