6

I'm on an iPhone project using ARC. The application is a navigation based one, using 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.

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...

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).

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" -> ...) ??

Thank you all in advance

Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
sonxurxo
  • 5,648
  • 2
  • 23
  • 33
  • ARC basically is used for the releasing the object automatically. You can create a new prohject with switching off the ARC so that you can release the objects manually. – Akhilesh Sharma May 22 '12 at 09:44
  • implement -(void)didReceiveMemoryWarning. This is the method that gets called when your app uses too much memory. – MJB May 22 '12 at 09:46
  • How are you implementing the back button behaviour? Can you post the code for this? – Jim May 22 '12 at 09:55
  • @CodemasterGabriel I know it's an option, but I *have to* use ARC in this case. – sonxurxo May 22 '12 at 10:06
  • @MJB I also have that method implemented, but the problem is that there is a lot of memory being used that should not be used at all, because the old controllers won't actually be shown anymore. – sonxurxo May 22 '12 at 10:09
  • @Jim No code for that, just normal UINavigationController behavior (BTW, thank you all for the quick comments) – sonxurxo May 22 '12 at 10:09
  • How are you instantiating your secondary view controller? Are you keeping a strong reference to it around anywhere? – Jim May 22 '12 at 10:15
  • I'm creating it at runtime (no ivar, no @property). – sonxurxo May 22 '12 at 10:30
  • @Jim should I have them as ivars and properties? – sonxurxo May 22 '12 at 13:46
  • @sonxurxo so where do you set nil on objects? – Dejell Feb 05 '13 at 15:26
  • I'm doing it on dealloc and viewDidUnload, and have no issues. The (old) problem I had was that there were reference cycles, so the memory didn't free – sonxurxo Feb 05 '13 at 18:47

1 Answers1

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.

Understanding Automatic Reference Counting in Objective-C

I hope this might help you out.

Akhilesh Sharma
  • 1,580
  • 1
  • 17
  • 29
  • In fact it does as to increase my knoweledge about ARC, thank you, +1 for that! But it does not solve my problem, I just need to, somehow, tell iOS that the instance of that secondary controller, every time the user press the back button, popping it from the navigation stack, will no longer be used. – sonxurxo May 22 '12 at 10:30