views:

6349

answers:

9

I wonder what the -(void)viewDidUnload is good for. Could I not just relase everything in -dealloc? If view did unload, wouldn't -dealloc be called anyways?

+7  A: 

As the documentation says:

It is called during low-memory conditions when the view controller needs to release its view and any objects associated with that view to free up memory.

In the same situation dealloc is not called. This method is only available in OS3 and above. Dealing with the same situation in iPhone OS 2.x was a real pain!

Stephen Darlington
Also from the docs: "You should do this only for objects that you can easily recreate later, either in your viewDidLoad method or from other parts of your application. You should not use this method to release user data or any other information that cannot be easily recreated". That's a question I had too myself, thanks!
leolobato
What if the view is currently visible? Wouldn't it be bad to drop it because of a low memory warning? ;) then the app would just be blank empty. I don't get the point of releasing the view because of low memory. If I don't see a view, I always release the whole controller. Althogh I have an root view controller which stays intact and manages all the loading/unloading of it's child view controllers...
Thanks
No, you wouldn't use this if you just swap one view for another. Think of the case where you have a "stack" of views with a UINavigationController. Only one view is visible and, if you have a memory warning, you can release all those that are not visible.
Stephen Darlington
How do you control that viewDidUnload is not called on the current visible view as Thanks has noted?
arielcamus
@arielcamus I guess you just have to trust the OS that it won't try to free a view that's currently visible. In practice this is something you don't need to worry about.
Stephen Darlington
@Stephen You are right... I've discovered iPhone OS is very clever with this kind of stuff. If something goes wrong it is usually your own fault.
arielcamus
viewDidUnload will not be called on the currently visible view, only on views that are not visible.
progrmr
Also be careful using self.view on a view controller, that getter causes the view to be reloaded. If it had been unloaded due to a memory warning you might accidently reload it (see isViewLoaded). I had a timer go off in an unloaded view and unnecessarily reloaded it.
progrmr
Is viewDidUnload called before dealloc or should I also release in dealloc what is released in viewDidUnload?
Kamchatka
+1  A: 

You can release any subviews you hold on to, for example that UIImageView you retained in your loadView method, or better yet the image that was on that UIImageView.

drvdijk
+4  A: 

Remember that viewDidUnload is a method in the view controller, not in the view. The view's dealloc method will get called when the view unloads, but the view controller's dealloc method may not be called until later.

If you get a low memory warning and your view isn't showing, which will happen for instance about any time you use a UIImagePickerController to let the user take a picture, your view will get unloaded and will need to get reloaded after that.

David Maymudes
that makes sense. What happens if I always drop the whole view controller? That's what I actually do. In this case I don't have to deal much with -viewDidUnload, right? I've never had the situation where I would drop only the view, since I always drop the whole controler if it isn't visible anyways.
Thanks
well, just remember that in a case where your view is showing, but you have a fullscreen view like the ImagePicker on top of it, your view might get unloaded even if you didn't plan for it to be.
David Maymudes
+13  A: 

In addition to what has already been indicated, I wanted to elaborate more about logic behind -viewdDidUnload.

One of the most important reasons for implementing it is that UIViewController subclasses commonly also contain owning references to various subviews in the view hierarchy. These properties could have been set through IBOutlets when loading from a nib, or programmatically inside -loadView, for instance.

The additional ownership of subviews by the UIViewController means that even when its view is removed from the view hierarchy and released to save memory, through which the subviews are also released by the view, they will not actually be deallocated because the UIViewController itself still contains its own outstanding retaining references to those objects as well. Releasing the UIViewController's additional ownership of these objects ensures they will be deallocated as well to free memory.

The objects that you release here are usually recreated and set again when the UIViewController's view is re-loaded, either from a Nib or through an implementation of -loadView.

Also note that the UIViewController's view property is nil by the time this method is called.

Sean Murphy
+2  A: 

Conclusion:

View Controllers have a view property. Typically a nib or piece of code adds other views to this view. This happens often inside a -viewDidLoad method, like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createManyViewsAndAddThemToSelfDotView];
}

in addition, a nib file may create a button and append it to the view controller's view.

On iPhone OS 2.2, when -didReceiveMemoryWarning was invoked from the system, you had to release something to free up memory. You could release the whole view controller's view if that made sense. Or just big memory-consuming contents in it.

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

Now, in the new OS 3.0, there is an -viewDidUnload method, which will be invoked from the system when the view has been unloaded because of low memory (please correct me: when exactly does this get called?)

-viewDidUnload is used to release all objects that were owned both by the view controller itself and the view. The reason: If a view controller holds references to childs of the view, i.e. a button, the referenced child views will not get released, because their retain count is >= 1. After they are released in -viewDidUnload, they can get freed up from memory.

Thanks
remeber in view did unload to do self.button = nil;, not [button release];.
Mk12
A: 

Why we should not do releases of objects and do only self.property = nil in a ViewDidUnload method?

JFMartin
Because, when the OS goes to re-hydrate your view (after a constrained memory situation while your view is in the background somewhere), if the property was released, but wasn't set to nil, the property semantics will attempt to re-release the (already destroyed) object when the property is set again... causing a crash. I found this out the hard way.
Steve
+1  A: 

This is because you will typically set the @property as "(nonatomic, retain)" and as such the setter that is created for you releases the current object and then retains the argument i.e.

self.property = nil;

...does something along the lines of:

[property release]; property = [nil retain];

Therefore you are killing two birds with one stone: memory management (releasing the existing object) and assigning the pointer to nil (since sending any message to a nil pointer will return nil).

Hope that helps.

gazzaaa87
A: 

What happens to the viewDidUnload method once the view controller´s dealloc method is called?

For example: When a view controller is popped from a navigation controller stack, the dealloc is called. Should I set all the views created in loadView to nil there too?

My guess is that the viewDidUnload is no good when dealloc is called, right?

Herman Olsson
A: 

If the view controller is popped from the navigation controller stack and is not retained anywhere else, it will be deallocated, and dealloc will be called instead of viewDidUnload. You should release the views created in loadView in dealloc, but it is not necessary to set the variables to nil, because soon after dealloc is called the variables will no longer exist.

Colin