I'm having problems releasing memory for view controllers, and hope someone can save my sanity! The app is really quite big, so for simplicity, I am adding code that just illustrates the problem.
I have an app that contains multiple views (each with corresponding view controller).
I have a MainViewController (inherits from UIViewController) which has a property for each sub-view controller:
Code:
@interface MainViewController : UIViewController {
SubViewController1 *subViewController1;
SubViewController2 *subViewController2;
SubViewController3 *subViewController3;
}
@property (nonatomic, retain) SubViewController1 *subViewController1;
@property (nonatomic, retain) SubViewController2 *subViewController2;
@property (nonatomic, retain) SubViewController3 *subViewController3;
-(void) openSubView1;
-(void) openSubView2;
-(void) openSubView3;
-(void) closeSubView1;
-(void) closeSubView2;
-(void) closeSubView3;
@end
In the implementation of MainViewController, I have the methods for opening and closing the subviews:
Code:
- (void) openSubView1 {
self.subViewController1 = [[SubViewController1 alloc] init];
[self.view insertSubview:self.subViewController1.view atIndex:0];
}
- (void) closeSubView1 {
[self.subViewController1.view removeFromSuperview];
[self.subViewController1 release];
self.subViewController1 = nil;
}
The app delegate has a property for the MainViewController, so the calls to the closeSubViewX methods are made via appDelegate.mainViewController from the relevant sub view controller implementation.
On the surface the functionality works as expected with the sub view showing and disappearing... however, the SubViewControllerX objects don't get released, and the retain count increases by 1 every time I call the openSubViewX methods.
The other interesting thing to note is that dealloc IS getting called on the SubViewControllerX - so I'm confused as to why the retain count doesn't reduce.
I really hope someone can help guide me on where I may be going wrong - thanks in advance!