tags:

views:

165

answers:

2

Hi all -

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

A: 

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

Tom Irving
Thanks for pointing me in the right directions folks. Turns out the exact culprit was some releasing done on objects that were not retained at construction (NARC violations as I believe they're called).
Wireless Designs
A: 

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.

TechZen
Well, scratch my idea. I wrote it before the second comment in the parent and apparently they aren't objects. The basic idea, however, is sound. The view is taking something out with it when it dies upon final release.
TechZen
Thanks for pointing me in the right directions folks. Turns out the exact culprit was some releasing done on objects that were not retained at construction (NARC violations as I believe they're called).
Wireless Designs