1

I am developing an application which has almost 12 view controllers. Application has tabBar with 4 tabs means four view controllers and there are multiple views to navigate in each tab bar. Please note each tab has a navigationController with a rootviewController being its firstview. Application is an extensive database application. When I run on simulator it works nice but get didRecieve memory warning while running on the device. I have few questions regarding the same.

NOTE: I have checked the application using Instruments tool for Leaks and there are no red pyramids which means there are no leaks in the code.

  1. What is the best practise when you deal with multiple view controllers.

  2. when I recieve memory warning, I call [self deleteObjects] which deletes all the instances which are retaning the values for the current controller. But I am not confident if this the right way. What should be done when we recieve memory warnings. Is there any good tutorial for that. (Links plz)

  3. Also how to make sure that the [self deleteObjcts] is not called for the visible controller. (I was calling the deleteObjects method in viewDidUnload method before but since we do [super didRecieveMemoryWarning] it breaks the code as viewDidLoad is called in hierarch from top to bottom so once I deleted object for top viewController obviously there will be error for rest of the controllers.)

Some of the basic confusing questions for me are as follows:

  1. Why the memory warnings are not consistent. Like I get them sometimes at the start whereas sometimes there are no warnings.

  2. When we used [NSDate date], [UIImage imageNamed:@"..."], [NSString stringWithFormat] etc, we dont own these objects and we dont have to release them but how can we make sure these objects are relased when we recieve a memory warning.

  3. I am using NSMutableArray at multiple places. In this arrays I store the [NSString StringWithFormat ], [UIImage imageNamed...] objects, so when I realease the arrays do I need to relase the objects in the arrays though I dont own them.

Though this is a big list of question but I appreciate your help and time since I am in last stage of my development I am facing these major challenges.

rkb
  • 10,933
  • 22
  • 76
  • 103
  • Just because Instruments doesn't see the memory leaks doesn't mean that they aren't there. You may want to give your code a pass through the Clang Static Analyzer to see if it finds anything. – Brad Larson Sep 28 '09 at 00:28

2 Answers2

1
  1. You receive memory warnings when you run out of memory. Memory is not only taken by your App, all other running processes use memory and the memory being used may always vary.
  2. Those object are all autoreleased. The NSAutoreleasePool will take care of releasing the objects, you should never release such an object yourself (well, not as long as you have not retained it yourself). This doesn't really matter, autoreleased object will be released quite quickly.
  3. When putting an object in an array, it will be retained. When you release the array, it will send release all it's children objects. As you store autoreleased objects in the array, they will take care of their own releasing. This has probably already happened when you release the array, so -dealloc will be called on all the objects immediately.

    NSMutableArray *someArray = [[NSMutableArray alloc] init];
    NSDate *date = [NSDate date]; // Autoreleased object. Retain-count is 1
    [someArray addObject:data]; // The array retains the data object. Now has a retain-count of 2
    // Some other things
    // The date object has been called release at some time (because it was autoreleased)
    // so date now has a retain-count of 1
    [someArray release] // Will release all containing objects thus date will be called dealloc
    
Joost
  • 10,333
  • 4
  • 55
  • 61
  • Regarding the autorelease pool, if you have a big (I mean huge) loop creating many autoreleasing objects, you might consider creating your own NSAutoreleasePool, since those objects will only get released after the cycle of the event loop created by Application Kit ends. This might help (has helped me a lot): http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html – leolobato Sep 27 '09 at 21:40
1

I've run into memory problems with UIImage imageNamed that I think are caused by the OS caching images and not releasing them when it should. There are lots of other developers who have seen the same thing.

I would try using imageWithContentsOfFile instead of imageNamed and see what happens.

For example forPNG images --

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename here" ofType:@"png"]];

Here's a thread that covers what I'm talking about:

Community
  • 1
  • 1