Are there dealloc and viewDidUnload methods when using ARC, pre-iOS6?

I work as part of a large team working on legacy iOS code with a target environment of iOS 4.3 and later. I saw other developers checking classes that descend from NSObject but don't have the dealloc method. I also saw UIViewController descendants that do not include viewDidUnload methods. When I ask about this code, the usual answer is: β€œDon't worry, ARC will take care of it now.”

I understand that viewDidUnload is called when iOS experiences low memory, in order to free memory by allocating objects that can be recreated by calling viewDidLoad , and that dealloc is called when the save counter of the object reaches zero. For UIViewController objects and children, this may mean that 'viewDidUnload' may or may not be called before dealloc .

So, here is my question: are dealloc and viewDidUnload methods viewDidUnload when using ARC in iOS versions prior to iOS 6?

If the answer is β€œ Yes! ”, Then I will need good reasons and / or documentation to transfer the argument.

Waiting for your answers. (Thanks Tommy for helping me pull up my question.)

+6
source share
2 answers

viewDidUnload deprecated . Therefore, regardless of ARC, you not only do not need one, but should not use it. The stated rationale is that the notions are no longer cleared of warnings about low memory (apparently because now they contribute too little to make the total amount worth the reaction to luck); I would not be surprised if part of the rationale was that many people assumed that they could free all the resources created in viewDidLoad inside viewDidUnload , and this alone would prevent leaks. This is not so because viewDidUnload is only called if the view has been unloaded due to a low memory warning . It is not called in the normal life cycle.

The New ARC Rules :

You can implement the dealloc method if you need to manage resources other than freeing instance variables. You do not need (indeed you cannot) free instance variables

EDIT: comment on 4.3+ specifically ...

ARC will not use the viewDidUnload version for you. The point of the viewDidLoad / viewDidUnload was that if you retain any part of the hierarchy of views for any reason, then you will be forced not to automatically issue a warning about low memory, t buy you any benefit, because as soon as the image will be downloaded, everything that you saved will be replaced with a new copy. Therefore, if you have a strong IBOutlet for views that are inside the hierarchy under self.view , in any case, ideally, you will lose them during viewDidUnload . Even if you have weak references, this is a good place to prevent wrapping any dangling pointers.

As in iOS 5, you may have weak links for self-updating, so using those and not implementing viewDidUnload will be a way if you support 5+. For 4.3, if you use strong links and omit viewDidUnload , you can end up preventing as a thorough response to the low memory warning like Apple, but you will not leak memory. If you use weak links, you need to be a little careful not to reference any of these objects at times when you may not have a view (i.e. any time you are not on the display, but a previously loaded view - setters on the controller, which also customize the presentation, but which are influenced by another, are a classic example, for example, if you update the field by observing the key).

You can use the Simulate Memory simulator to test and debug this material to a degree.

dealloc that ARC provides will be the same regardless of the iOS version. However, it will only cover Objective-C objects. When they say that you cannot release instance variables, they mean that, in the most literal sense, send a release message to them. Suppose you have Core Foundation objects or have a clean C allocation, then you want to implement dealloc , which has all of these.

Obviously, the Leaks tools and tool are ways to test and debug this area; be careful if any time memory is skipped to check if the type of object that created this memory is leaking. The immediate object may be accurate, but its distributions will appear in the missing list if it is not dealloc , because someone else has missed it.

+14
source

viewDidUnload now deprecated and is no longer called by the system (starting with iOS 6). It was never as useful as Apple had hoped, and there was more trouble than it was worth. This has nothing to do with ARC.

dealloc often not required in ARC, but it is still necessary in situations where you need resource management without ARC. For example, if you need to use free() or otherwise free the resource. It is also a good place to remove yourself as an observer or delegate. But for many classes dealloc is no longer required.

+6
source

Source: https://habr.com/ru/post/926451/


All Articles