Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an iOS app's app delegate need to retain the UIWindow?

In the Xcode templates for iOS apps such as the 'View Based Application', a MainWindow nib is created with contains three top-level objects: The App Delegate, the Window, and the main View Controller. The App Delegate defines retain outlets/accessors for both the window and the view controller. I don't understand why the App Delegate would need to retain these objects since they are already top-level objects in the nib and therefore should have been retained by UIApplication. Checking the retainCount of these objects sure enough shows 1 for the app delegate and 2 for the window and view controller. Could I/should I change these to assign instead?

I know this is a nit picky thing but I'm hoping that understanding why this is done will improve my overall understanding of memory management in iOS.

like image 709
Marplesoft Avatar asked Aug 17 '11 18:08

Marplesoft


People also ask

What is difference between app delegate and scene delegate?

The app delegate, by default, will use the default scene configuration. The scene delegate, by default, sets up a UIWindow object, and uses the Main. storyboard to create the initial UI.

What is app delegate in iOS?

So an app delegate is an object that the application object can use to do certain things like display the first window or view when the app starts up, handle outside notifications or save data when the app goes into the background.

Can we have multiple UIWindow in iOS?

Yes, you can have multiple windows. A key window is the one who receives the user input.

What is UIWindow in iOS?

The backdrop for your app's user interface and the object that dispatches events to your views. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


1 Answers

Assign is tricky in iOS with outlets I use @property (nonatomic, retain). Unlike in Mac OS, connected outlets to XIB objects in iOS are not automatically retained and memory managed, this may change with iOS 5, but is somewhat unlikely.

The rationale there is so that you can release any view objects in - (void)viewDidUnload, and get rid of any views that you either don't need, or can re-initialize on -(void)viewWillAppear. But the goal is, ostensibly, to keep you in control of what is collected and what is not.

my pattern is to just declare normal cocoa accessors for them as I would for any other properties and set them to nil in the viewDidUnload

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.buttonOne = nil;
    self.buttonTwo = nil;
    self.buttonThree = nil;
    self.buttonFour = nil;
    self.buttonFive = nil;
    self.buttonSix = nil;
    self.lineWidthSlider = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

You are right, however in that it doesn't really make much sense to have the retain for the window, but it makes sense for consistency IMHO. So long winded way of saying yes, in my experience the app delegate does need to retain the UIWindow or it can be collected in a memory sweep and cause somewhat random crashes.

like image 149
Heat Miser Avatar answered Oct 28 '22 02:10

Heat Miser