Code EXC_BAD_ACCESS 2 on UIAlertView in iOS6

I am trying to understand why I am getting this crash in my application.

It works fine in Xcode 4.4, running in a simulator with ios5.1, but when I switch to xcode 4.5 and ios6, I get the code EXC_BAD_ACCESS 2. Here is my code:

- (void) myMethod { UIAlertView *alertview = [[[UIAlertView alloc]initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil] autorelease]; alertview.tag = 1 [alertview show]; } 

this gives me the code EXC_BAD_ACCESS 2 in the line [UIAlertView show]

any ideas?

thank!

+43
ios6 uialertview exc-bad-access
Sep 17 '12 at 23:57
source share
2 answers

I have it. I have the same problem, in my case it seems that the method is now thrown out of the background (now in ios7, in ios6 the UIAlertView was automatically placed in the main stream, as @nodepond says -thanks! -) ..

try to make sure that the method is shown from the main thread:

 [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES]; 

Good luck

+126
Sep 18 '12 at 11:12
source share

It happened to me, even in 2014. The problem is that you want to use an already released object.

What have I done wrong:

 //class B with UIAletViewDelegate -(void) showAlert{ UIAlertView * alert = [[UIAlertView alloc] initWithTitle bla bla...]; [alert show]; } //class A viewDidLoad{ MyClassB *B = [[B alloc] init]; [B showAlert]; } 

What is the correct way:

 //Class A @implementation A{ ClassB *B; } viewDidLoad{ B = [[B alloc] init]; [B showAlert]; } 
0
Oct 21 '14 at 2:23
source share



All Articles