0

I am quite beginner in development and I'm making an application that have about 150 ViewControllers ! Each one have a UIImageView.

I've been doing a bit of testing, and after a use the app for a while on the iPhone itself, it Crashes when I keep viewing the ViewControllers.

I've been analyzing this in instruments, and I have no leaks, however my memory allocation just goes up and up and when I keep viewing the ViewControllers on my iPhone the usage just goes up and up until a crash.

I think it's clear that it's crashing because the app is simply taking up too much memory.

So could any one explain how to release viewControllers in order to free up the memory so there will be no crash

Thanks in advance !

1 Answers1

0

The first question you need to ask yourself is why do you need 150 ViewControllers. Do you want to present 150 different images? If so, then multiple ViewControllers is not the way to go. You probably need to use a UIScrollView that will contain your images (not all of them at once, of course. Use lazy loading).

What's probably happening in your case is that you call "pushViewController" each time you need to display a new ViewController, but this doesn't release the previous ViewController. It simply stacks all of the previous ViewControllers and retains their pointers.

You see, the way Navigation Controllers work is that they have an array of view controllers. Every time you present a new view controller, it is added at the end of the array. When you click on "Back" or call "popViewController" the last item of the array is removed (and then released automatically from memory).

See this and this questions to learn how to create a UIScrollView to scroll images.

Community
  • 1
  • 1
Eli Ganem
  • 1,479
  • 1
  • 13
  • 26
  • Thank you so much for your answer .. I knew before that I shouldn't use multiple viewControllers .. first I will give a try for calling "pushViewController" will it be useful ? then I will re-design my app and I will use UIScrollView .. Thank you so much again ! :) – Abd Elrhman Rizk Nov 02 '12 at 22:49
  • @AbdElrhmanRizk No, you shouldn't use "pushViewController" as it will keep a pointer to your previous ViewControllers. You should either use UIScrollView or simply replace the image you're currently presenting with the next image you want to show. i.e. `myImageView.image = [UIImage imageNamed:@"NewPicName.png"]` – Eli Ganem Nov 02 '12 at 23:03
  • Well, I will use UIScrollView .. Thank You for your time :) .. Good Luck ;) – Abd Elrhman Rizk Nov 02 '12 at 23:24