简体   繁体   中英

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:@"MatcherView" 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

在控制器的dealloc方法中,确保您没有释放过多的内容。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM