views:

158

answers:

1

When an application comes back from low memory conditions (ie there was low memory, things were freed and the app is now back to normal use scenario), what happens to the state of objects that were initialized and set up via

-(id)init

method?

When you receive low memory warnings, you persist all of the data and the viewDidUnload method is invoked. Eventually, the view maybe reloaded but the class's 'init' method is not called a second time.

So would you persist any state information you have initialized in the 'init' method and later manipulated during the course of the use of the application?

What I'm asking more specifically is whether classes & other related data created during the 'init' method would be reinstated when coming back from a low-memory condition.

+1  A: 

If an object is dealloc'ed you got a memory alert, then you have to do the init again. It will not be done for you.

The system will typically not dealloc your objects unless you do so by releasing. You could do nothing in response to a memory alert or you could release some views and with them, some of their owned objects.

viewDidUnload is a way for you to know whether your view was unloaded in which case you should go through and free and cleanup the things that you did in viewDidLoad. When your view comes back up, viewDidLoad will get called again and you get a chance to redo all the initialization.

viewDidUnload is probably what you really need study:

viewDidUnload Called when the controller’s view is released from memory.

  • (void)viewDidUnload

Discussion This method is called as a counterpart to the viewDidLoad method. It is called during low-memory conditions when the view controller needs to release its view and any objects associated with that view to free up memory. Because view controllers often store references to views and other view-related objects, you should use this method to relinquish ownership in those objects so that the memory for them can be reclaimed. You should do this only for objects that you can easily recreate later, either in your viewDidLoad method or from other parts of your application. You should not use this method to release user data or any other information that cannot be easily recreated.

Typically, a view controller stores references to objects using an outlet, which is a variable or property that includes the IBOutlet keyword and is configured using Interface Builder. A view controller may also store pointers to objects that it creates programmatically, such as in the viewDidLoad method. The preferred way to relinquish ownership of any object (including those in outlets) is to use the corresponding accessor method to set the value of the object to nil. However, if you do not have an accessor method for a given object, you may have to release the object explicitly. For more information about memory management practices, see Memory Management Programming Guide for Cocoa.

By the time this method is called, the view property is nil.

Special Considerations If your view controller stores references to views and other custom objects, it is also responsible for relinquishing ownership of those objects safely in its dealloc method. If you implement this method but are building your application for iPhone OS 2.x, your dealloc method should release each object but should also set the reference to that object to nil before calling super.

mahboudz