Memory not showing in ios view hierarchy

I have an iOS application that uses ARC. I do not use InterfaceBuilder, the entire user interface is created manually. In this application, I have several UIViewControllers with SubViewControllers. These ViewControllers are linked from the menu (-ViewController), which pushes them onto the stack.

My problem is that memory is not freed when switching between ViewControllers.

Is it wrong to maintain references to SubViewControllers as follows?

@property (nonatomic, strong) UIViewController subViewController1; @property (nonatomic, strong) UIViewController subViewController2; 

viewDidUnload is never called. Does anyone have a good example of how to build a clean hierarchy of views?

+4
source share
2 answers

This, most likely, has nothing to do with ARC. viewDidUnload is called only in the view controller when the view property is released / set to nil, and this usually happens only if the application receives a memory warning.

Try running a memory warning in the simulator and see if this calls your viewDidUnload method. If this happens, everything will be fine. If not, you are probably holding back your views too much, possibly assigning them to other highly preserved properties.

There are exceptions to the view preservation policy, for example, the UINavigationController frees views in the view controller stack if they are not the earliest, but it does this by simply setting the view of its child controllers to zero, overlapping another view of the controller.

If you want your views to be released when they were not on the screen, either set the controller view property to nil in the viewDidDisappear method: or stop saving the view controllers when their views are not displayed on the screen, just create a new controller each the time you need to display them (this way, both the controller and the view will be released if they are not used).

0
source

By assigning view controllers that hit the stack to a strong instance variable / property, they will not be freed when removed from the stack. Strong properties hold onto pushed view controllers even after they slip out of the stack, so they never end up in a state in which they can be freed.

Usually I do something like the following when you push the next level view controller down on the navigation stack:

 SLSMoleculeSearchViewController *searchViewController = [[SLSMoleculeSearchViewController alloc] initWithStyle:UITableViewStylePlain]; [self.navigationController pushViewController:searchViewController animated:YES]; 

In ARC, the new view controller will be highlighted and saved at creation. When you click on the navigation stack, it will be held once by the navigation controller. Since this new view controller is not mentioned after clicking and is not bound to a strong property or instance variable, ARC will release it after the second line.

Remember that it is still stored by the navigation controller, so it still lives in memory. However, as soon as the navigation controller pushes it from the stack, this controller will be freed. Since nothing is holding at this moment, it will be released as expected.

If for some reason you need to maintain a link to this subview controller in your higher level view controller, you can use the weak property or the __weak instance __weak . This will not be held by the view controller and will be turned off when the controller is released.

weak links are only supported for apps targeting iOS 5.0, so you won’t be able to do this for something that needs to be run on iOS 4.0. The 4.0 compliant unsafe_unretained property unsafe_unretained not something I would recommend in this case because of the danger of a pointer to freed memory.

+5
source

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


All Articles