Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click on UIAlertView crashes app if view is dismissed

A UIAlertView is displayed if an error occurs. But in the meantime the view on which the UIAlertView were called has been dismissed (and therefore released). If the user clicks on OK the app crashes because a message to a released instance is sent. This will cause your app crashing:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"test" message:@"test" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
alertView = nil;
[self.navigationController popViewControllerAnimated:YES];

I thought the UIAlertView is an independent unit. But it seems it isn't. Is there a way how I could avoid the app crashing (except not dismissing the view)?

like image 333
testing Avatar asked Oct 14 '10 23:10

testing


1 Answers

The delegate is called when the UIAlertView is dismissed, so in your case:

delegate:self

Delegates are not retained, like an object added to an array, or a subview would be. So in your case, when you call:

[self.navigationController popViewControllerAnimated:YES];

self is most likely being released, and when the the user dismisses the alert, self is called, but has been dealloc'd so it no longer exists.

An easy way to check this is to put a logger statement, like NSLog(@"I'm gone"); in self's dealloc method, if it's ran, then you know your self isn't around anymore, and any messages sent to it will cause a crash.

like image 144
SooDesuNe Avatar answered Sep 21 '22 17:09

SooDesuNe