Henley Wing Chiu
Henley Wing Chiu

Reputation: 22535

Is it safe to call [self <>] inside viewDidUnload?

Is it safe to do something like:

- (void) viewDidUnload {
   [self cleanup] ;
   [super viewDidUnload];
}

Because I hear things such as "self" may not even exist in a non-corrupt form when the view is unloading, hence it may be unsafe. Is it safe?

Upvotes: 2

Views: 81

Answers (1)

cduhn
cduhn

Reputation: 17918

It's totally safe. viewDidUnload gets called in low memory situations when the view controller has temporarily deallocated the view to conserve memory. You just don't want to do anything in there that would access your controller's view property, as that would cause the controller to lazy-load the view again. Other than that, calling methods on self in viewDidLoad is totally safe.

You may be thinking of the dealloc method. I've seen people assert that you shouldn't call methods on self in your init or dealloc methods because your object may be in a partially initialized/released state. I guess the fear is that someone will try to modify or override the method you're calling without realizing that it's being called on a partially-formed object. This risk is increased if you don't set your released properties to nil in dealloc.

Upvotes: 5

Related Questions