使用ARC时将对象设置为nil?

时间:2022-04-14 09:44:17

I'm on an iPhone project using ARC. The application is a navigation based one, using UINavigationController.

我正在使用ARC的iPhone项目。该应用程序是基于导航的应用程序,使用UINavigationController。

The typical use case is to go from a "main" UIViewController to a "secondary" UIViewController multiple times, maybe up to 100 times. The secondary controller has a lot of static, local PNG images, some of them repeated.

典型的用例是从“主”UIViewController转到“辅助”UIViewController多次,可能多达100次。辅助控制器有很多静态的局部PNG图像,其中一些是重复的。

I'm profiling the application and I can see how, when going from "main" to "secondary" controller, it allocates about 0.8 MB, but, when pressing the back button, it does not free the memory. So, when I go again to the secondary controller, other 0.8 MB are allocated, and so on...

我正在分析应用程序,我可以看到,当从“主”控制器转到“辅助”控制器时,它分配大约0.8 MB,但是,当按下后退按钮时,它不会释放内存。所以,当我再次进入辅助控制器时,会分配其他0.8 MB,依此类推......

Debugging, I noticed that viewDidUnload: method of the secondary UIViewController is never being called, but I also read that it's that method where I'm supposed to set to nil the references kept by the controller. Doing so in viewDidDisappear: does not help, because I want that to occur only when pressing the back button, that is, when popping the controller from the stack (the viewDidDisappear: method would be also called when pushing another controller on the stack).

调试时,我注意到从未调用辅助UIViewController的viewDidUnload:方法,但我也读到了那个我应该设置为nil控制器保存的引用的方法。在viewDidDisappear中这样做是没有用的,因为我希望只有在按下后退按钮时才会发生这种情况,也就是说,当从堆栈中弹出控制器时(在按下堆栈上的另一个控制器时也会调用viewDidDisappear:方法)。

So the questions are: is there where I have to do that? Can I force that method to be called? Is that behavior OK (profiling, the allocations went up to 20MB after some cycles of "main" -> "secondary" -> "main" -> "secondary" -> ...) ??

所以问题是:我有什么必须这样做的吗?我可以强制调用该方法吗?这种行为是否正常(分析,在“主要” - >“次要” - >“主要” - >“次要” - > ......的某些周期后,分配上升到20MB?

Thank you all in advance

谢谢大家

1 个解决方案

#1


1  

ARC is just used to reduce the code and manage the memory internally. For more details please go through the tutorial below so that you are able to understand the concepts more easily.

ARC仅用于减少代码并在内部管理内存。有关详细信息,请阅读以下教程,以便您能够更轻松地理解这些概念。

Understanding Automatic Reference Counting in Objective-C

了解Objective-C中的自动引用计数

I hope this might help you out.

我希望这可以帮助你。

#1


1  

ARC is just used to reduce the code and manage the memory internally. For more details please go through the tutorial below so that you are able to understand the concepts more easily.

ARC仅用于减少代码并在内部管理内存。有关详细信息,请阅读以下教程,以便您能够更轻松地理解这些概念。

Understanding Automatic Reference Counting in Objective-C

了解Objective-C中的自动引用计数

I hope this might help you out.

我希望这可以帮助你。