Found an odd bug tonight in an iPhone application I’ve been tinkering with.
The problem was that I had some code in a main UIViewController which was resizing the contents of it’s view. The code was being triggered by a button in another UIViewController which was placed in front of my main UIViewController through -presentModalViewController:animated:. And the code was crashing with a segment violation.
Some research later, and I figured out the problem: if you have a view controller representing a view hierarchy that is not currently on the screen (because it’s been kicked into the background by presentModalViewController), the view data structures can be unloaded from memory.
When this happens, calling [view bounds] on a view in your background view controller will crash, because the bounds is no longer present in memory.
Lesson: if a dialog or foreground view controller causes the background view controller to rearrange itself, you are better off detecting the change after the dialog or foreground view controller dismisses itself. In other words, only update views when they are visible.