lostintranslation
lostintranslation

Reputation: 24583

Dealloc not getting called

I am trying to debug why my override of dealloc is not getting called on one of my view controllers.

I have a view controller that is setup by storyboarding. I have override all 3 init methods:

-(id) initWithCoder:(NSCoder *)aDecoder {
    NSLog(@"TestViewController init with coder");

    return [super initWithCoder:aDecoder];
}

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    NSLog(@"TestViewController init with nib name");

    return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}

- (id) init {
    NSLog(@"TestViewController init");

    return [super init];
}

When I navigate to the said view in my app I see that 'initWithCoder' is called as expected. This is the only init method that I see called at all. It is called once when I navigate to my view.

However when I navigate away from my view controller dealloc does not get called. viewDidDisappear does get called. In view didDisappear I log the reference count:

-(void) viewDidDisappear:(BOOL)animated {
    NSLog(@"TestViewController Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef) self));
    [super viewDidDisappear:animated];
}

Reference count is 5.

I also log the reference count in viewDidLoad and it is 8.

So it jumps from 5 to 8, then back to 5.

I only see the init method called once. I have no idea where to even start looking....

Upvotes: 0

Views: 5632

Answers (1)

Nick Wilkerson
Nick Wilkerson

Reputation: 385

Do you have an instance variable pointing to your viewController object? If you still have a pointer it will not decrement the retain count even if the view is not on the screen.

The unusual retain counts are normal. The only count that matters is 0. If it is 0 it will be deallocated. There is no guarantee that one pointer means there will be a retain count of 1.

Upvotes: 2

Related Questions