0

There are a few instances where my app crashes when turned back on from sleep and the app is still open. It happens if a UITableViews is open when the iPhone is put into sleep; the table is being filled from an XML being parsed. I understand why it is crashing; in my viewDidUnload method I am releasing the array that is filling the table. How to handle this I am not sure; yes I could simply not release it in viewDidUnload, but then it would never leave memory if you returned to the main menu.

Any help would be appreciated!

RyanG
  • 4,393
  • 2
  • 39
  • 64
  • Can you post your code? If your problem is that you're releasing the array and it is still being filled even when you leave the view, then you need to have a way of stopping your XML parsing mechanism. Releasing the array in itself should not be a problem... – André Morujão Apr 15 '11 at 12:21

1 Answers1

1

The method viewDidUnload is not the right place to release your data if at all. I quote Apple's documentation, which desribes it better than I could:

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.

ViewDidUnload is used only to release view related objects. A view controller can release its view because its not shown, still your instance of that controller exists and so does your model.

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
  • Thanks I wasn't fully understanding the viewDidUnload method; removing the array from being release in here does fix the problem. I am still releasing the array within dealloc. So I should still release my buttons/labels etc. in viewDidUnload correct? – RyanG Apr 15 '11 at 12:39
  • Yes any extra reference you are having on your view elements. The superview retains the subviews and of course your retained properties, so it is important to release them. – Nick Weaver Apr 15 '11 at 12:48