Note that there are some explanatory texts on larger screens.

plurals
  1. POUINavigationController crash because of pushing and poping UIViewControllers
    primarykey
    data
    text
    <p>My question is related to my discovery of a reason for UINavigationController to crash. So I will tell you about the discovery first. Please bare with me. </p> <p><strong>The issue:</strong> I have a UINavigationController as as subview of UIWindow, a rootViewController class and a custom MyViewController class. The following steps will get a Exc_Bad_Access, 100% reproducible.:</p> <pre><code>[myNaviationController pushViewController:myViewController_1stInstance animated:YES]; [myNaviationController pushViewController:myViewController_2ndInstance animated:YES]; </code></pre> <p>Hit the left back tapBarItem twice (pop out two of the myViewController instances) to show the rootViewController. </p> <p>After a painful 1/2 day of try and error, I finally figure out the answer but also raise a question.</p> <p><strong>The Solutio:</strong> I declared many objects in the .m file as a lazy way of declaring private variables to avoid cluttering the .h file. For instance, </p> <pre><code>#impoart "MyViewController.h" NSMutableString*variable1; @implement ... -(id)init { ... varialbe1=[[NSMutableString alloc] init]; ... } -(void)dealloc { [variable1 release]; } </code></pre> <p>For some reasons, the iphone OS may loose track of these "lazy private" variables memory allocation when myViewController_1stInstance's view is unloaded (but still in the navigation controller's stacks) after loading the view of myViewController_2ndInstance. The first time to tap the back tapBarItem is ok since myViewController_2ndInstance'view is still loaded. But the 2nd tap on the back tapBarItem gave me hell because it tried to dealloc the 1st instance. It called [variable release] resulted in Exc_Bad_Access because it pointed randomly (loose pointer). </p> <p>To fix this problem is simple, declare variable1 as a @private in the .h file. </p> <p><strong>Here is my Question:</strong> I have been using the "lazy private" variables for quite some time without any issues until they are involved in UINavigationController. Is this a bug in iPhone OS? Or there is a fundamental misunderstanding on my part about Objective C? Please help. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload