开发者

EXEC_BAD_ACCESS when Dismissing ModalViewController

I'm using a pretty standard recipe for presenting ModalViewControllers in my iPhone apps, but I've run across a situation where the recipe is broken and I'm confused. This is how I (pretty much always) set up the presentation:

MatcherViewController *controller = [[MatcherViewController alloc] initWithNibName:@"M开发者_如何学编程atcherView" bundle:nil];
[controller setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[controller setDelegate:self];
[self presentModalViewController:controller animated:YES];
[controller release];

This always works great until I added one thing to the mix, and I sent a message to the new controller object before I presented it, like so:

MatcherViewController *controller = [[MatcherViewController alloc] initWithNibName:@"MatcherView" bundle:nil];

[controller setPrimary:primaryIndex andSecondary:secondaryIndex];

[controller setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[controller setDelegate:self];
[self presentModalViewController:controller animated:YES];
[controller release];

Adding this method call seems to work until I dismiss the view controller...at which point the app crashes with an EXEC_BAD_ACCESS signal. I can get it to work with the extra line if I remove [controller release], but then I'm afraid that will cause a leak. Any ideas why sending a message to the object prior to presentation would cause this? Is there a better way to pass simple parameters up to the ModalViewController?

Thanks for your time in straightening out the newbie ;p


I'm going to guess that primaryIndex and secondaryIndex are improperly retained objects such that they depend on the modal view retaining them to survive. When you release the modal view, they die but are then called somewhere else in the code causing the crash.

If they are retained properties of the class, always access them with the "self.propertyName" construction to make sure their retain counts are properly managed.

In my experience premature optimization in the form of over releasing is a major cause of Objective-C headaches today. Old school Objective-C coders were paranoid about leaks because they were almost impossible to track down by hand back in the day. That is why a lot of the resources still put so much emphasis on preventing leaks as you go. However, with modern analyzing tools, leaks are usually trivial to track down.

During initial development, when in doubt don't release.


In your controllers dealloc method, make sure you're not over releasing anything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜