2

I have a large hierarchy of view/viewcontrollers.
In the main controller I have the following code where aViewController is a member of MyClass:

@implementation MyClass

...

- (void) viewDidLoad
{
    [self.view addSubview:aViewController_.view];
    [aViewController_ setDataSource:self];
    [aViewController_ setDelegate:self];
}

- (void)dealloc 
{
    //[aViewController_.view removeFromSuperview]; // All ok when this is added
    [aViewController_ release];
    [super dealloc];
}

...

@end

When running this, I see that aViewController is never released - retain count remains 1 at the end.
If howeevr I add [aViewController_.view removeFromSuperview]; to dealloc the everything works fine.

Why is this? Shouldn't [super dealloc] take care of the release of the view? Does it matter that the view is being released after the controller?

I have tried to reproduce with a simple test application without any luck.

Egil
  • 4,265
  • 4
  • 20
  • 13
  • I think [super dealloc] will take care of removal of your view. But as it is called at the last so you are unable to check aViewController_.view's retain count correctly. – Ravin May 10 '11 at 04:11
  • [super dealloc] will call dealloc on the parent class so it can do any clean up it needs to do. The property "view" if it is being retained elsewhere (by a superview) may not be removed from its super view just because the controller has been released. – Steve M May 10 '11 at 04:23
  • It's hard to give an answer when it's not entirely clear who allocated aViewController_. Can you update your question to include this information? – csano May 10 '11 at 04:24
  • As I wrote - I also presume that [super dealloc] should handle this but in Instruments I see otherwise - and the fix described solves the problem. I just don't understand why... – Egil May 10 '11 at 04:27

1 Answers1

3

This is adding your view to be a subview of aViewController_.view.

[self.view addSubview:aViewController_.view];

aViewController_.view is retaining the view so when you release your main controller the view does not get deallocated that is correct.

You need to remove the view from the superview before you deallocate your "main controller".

The "main controller" will be gone when you release it but the super view still has retention of the subview.

You can do this somewhere in the aViewController_ when your finished with the view before you release the controller that owns the view.

Its hard to say without seeing all your code.

Just remember that when you add the view to another view that the super view retains the view until it is removed.

Robin
  • 10,011
  • 5
  • 49
  • 75
Steve M
  • 1,232
  • 9
  • 4
  • 1
    Also, you shouldn't be using the retain count to determine whether the object is being released. Objects aren't always released right away. – csano May 10 '11 at 04:15
  • I am checking the retainCounts in 'Instruments' by following the whole lifecycle – Egil May 10 '11 at 04:18
  • I clarified the example a touch. The line [self.view addSubview:aViewController_.view]; is adding aViewController_.view while your answer is describing the opposite situation. – Egil May 10 '11 at 04:22
  • I would add that using 'Build and Analyse' in Xcode will helpfully flag these kinds of issues – AJ. May 10 '11 at 04:35
  • @AJ: This is where I found that it was not being released – Egil May 10 '11 at 04:40