iOS: why should I deallocate first in viewDidUnload and then dealloc methods?

有些话、适合烂在心里 提交于 2020-02-29 06:19:47

问题


I've found on Apple documentation pages an example in which they deallocate the memory as follows:

- (void)viewDidUnload
{
  self.mapAnnotations = nil;
  [super viewDidUnload];

  // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
  // For example: self.myOutlet = nil;
}

- (void)dealloc
{
  [mapAnnotations release];
  [super dealloc];
}

I was wondering why

  1. they first set mapAnnotation to nil in viewDidUnload and then they release in dealloc method and

  2. why they refer using to mapAnnotation with and without self.

This is the downloadable example by the way: MapCallouts Example


回答1:


The question you should be asking is: when is viewDidUnload called?

In short it's called in low memory situations where you don't want to deallocate the whole controller, i.e., when you don't want dealloc to be called.

You can think of viewDidUnload as the opposite of viewDidLoad of loadView whereas dealloc is the opposite of init. (In practice it can get a bit more complicated than that of course.)




回答2:


viewDidUnload can be thought of as the opposite of viewDidLoad. It is called in cases where the view is unloaded due to memory warnings, but the view controller is not actually deallocated.

It allows you to release memory that is only relevant when the view is loaded, and therefore allows you to free up memory in these low memory conditions.

As for the difference between the two releases, one using self and one not:

In the viewDidUnload method, the variable is being set to nil via it's accessor methods. The variable has been declared as a property, likely with the retain attribute. When assigning nil via a property, it's functionally the same as the following code:

- (void)setMyObject:(MyObject *)object
{
    [myObject release];
    myObject = [object retain];
}

So if you're passing nil to that method, you'll be releasing the old instance and assigning nil to the variable. Assigning nil after releasing is considered good practise to prevent any possibility of trying to dereference a dangling pointer.

In the dealloc method, the variable is accessed directly, as opposed to via an accessor method or property, and is released. There is not really any need to assign nil at this point because the owning object will be deallocated and will not be accessing any of its pointers, making the possibility of dereferencing a dangling pointer very low.




回答3:


(1) This is done so you can let go of references so the system can deallocate resources in low memory situations. It is the reciprocal of viewDidLoad:

(2) If you refer to self using dot notation, you will maybe create an object that is the implementation detail of the property. Using the name ensures that you release the object itself, and not the object returned by the property. In the first example, in the case of an outlet, assigning nil with the dot notation (self.) will decrement the retain count of the retained property.



来源:https://stackoverflow.com/questions/5981375/ios-why-should-i-deallocate-first-in-viewdidunload-and-then-dealloc-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!