sirab333
sirab333

Reputation: 3722

Please assist with modalViewController crash

My app is crashing when I dismiss a ModalViewController via:

[self.parentViewController dismissModalViewControllerAnimated:YES];

This modal view-controller ("MVC") is presented when a user clicks on one of the cells of a UINavigationController ("NavRoot") - here's the code for that:

MVC *modalView = [[MVC alloc] initWithNibName:@"MVC" bundle:nil];
[self.navigationController presentModalViewController: modalView animated:YES];
[modalView release];         

The "modalView" which is loaded contains only 2 objects: a UIWebView object and a "DONE" button, which when clicked-on does the dissmissing via:

[self.parentViewController dismissModalViewControllerAnimated:YES];

Except when I click on "DONE" - the app crashes.

When I run Instruments with NSZombies I do see the retain count reaches -1 but I can't tell what's causing this over-release.

The only thing I found which solves the problem is to either add a "[modalView retain]" statement in "NavRoot" - which is the viewController doing the presenting of modalView:

MVC *modalView = [[MVC alloc] initWithNibName:@"MVC" bundle:nil];
[self.navigationController presentModalViewController: modalView animated:YES];
[modalView retain];  //  <<== new 'retain' statement
[modalView release];

or just simply never releasing modalView in the first place:

MVC *modalView = [[MVC alloc] initWithNibName:@"MVC" bundle:nil];
[self.navigationController presentModalViewController: modalView animated:YES];
// commenting out the 'release':
// [modalView release];

Both of these options throw flags when I run "Analyze" ("Potential leak of an object allocated on line 34"...) but they do fix the problem. Still, I worry about this causing the app to be rejected by Apple from the App Store.

Any ideas on what may be causing the over-release? Or how I might further try to isolate / identify the problem?

attaching an image of Instruments/Zombies report: enter image description here

Upvotes: 0

Views: 353

Answers (4)

Tanin
Tanin

Reputation: 1923

[self dismissModalViewControllerAnimated:YES] does not work on iOS 5.

I have built a category that add presentingViewController on iOS 4. (It disables itself on iOS 5.)

Just include 2 files, and it works seamlessly.

Please see backward-modal.

I hope this benefits you as much as it does to me; It makes your code more clean!

Upvotes: 0

Souljacker
Souljacker

Reputation: 774

To dismiss a modalViewController I simply just do: [self dismissModalViewControllerAnimated:YES];.

Upvotes: 0

Craig Mellon
Craig Mellon

Reputation: 5399

Are u using iOS 5? I had the same problem when I switched an app from ios4 to 5.

ParentViewController is now called presentingViewController

What you can do though is in your modal view just call [self dismissModalViewController] and it should dismiss itself. I'm not 100% about that and can't check as I'm not near my mac, but I recall reading it in the docs,

Upvotes: 1

craig1231
craig1231

Reputation: 3867

If you do

[self.navigationController presentModalViewController: modalView animated:YES];

Then you should dismiss it like

[self.navigationController dismissModalViewControllerAnimated:YES];

Rather than

[self.parentViewController dismissModalViewControllerAnimated:YES];

Where are you trying to dismiss the view from? The actual modalView or the parentView? It seems to me that you are trying to dismiss a modal view that has already been dismissed and subsequently released.

Upvotes: 0

Related Questions