43

I'm trying to figure out why im getting this crash in my app.

It works perfectly fine in Xcode 4.4 running in the simulator with ios5.1, but when i switch into xcode 4.5 and ios6 I'm getting an EXC_BAD_ACCESS code 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 is giving me an EXC_BAD_ACCESS code 2 on the [UIAlertView show] line

any ideas?

thanks!

Sean Danzeiser
  • 9,141
  • 12
  • 52
  • 90

2 Answers2

127

I've got it. I have the same problem, in my case it seems that the method is thrown from background now (now in ios7, in ios6 UIAlertView was automatically put into the main-thread as @nodepond says -thanks!-)..

try to assure that the method is shown from main thread:

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

Good luck!

Eva Madrazo
  • 4,731
  • 2
  • 23
  • 33
  • 3
    I just want to add in that this crash also happens when the app is launched, if user goes to home and comes back, an alert view show will result in crashing. Doing performSelectorOnMainThread... will fix it as suggested by Eva. – Tommy Sep 24 '12 at 04:42
  • 1
    I think this has changed with iOS6.0. Before that, the UIAlertView was automatically put into the main-thread. – nodepond Feb 01 '13 at 16:41
  • i gave +1, for what i was looking for, but the main reason for happening this kind of problem is multithreading, when you are performing some thing on separate thread that is not main thread, and in thread you are accessing or modifying GUI as GUI functionality only works on main thread. – umer sufyan Nov 27 '13 at 06:40
  • thanks for this answer, i was confused of why it would go well in os7+ but crashed on 6 – Yonathan Jm Sep 09 '14 at 03:57
0

It's happened with me, even in 2014. The problem is want to use an object already released.

What I did 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 right way:

//Class A
@implementation A{
    ClassB *B;
}

 viewDidLoad{
     B = [[B alloc] init];
     [B showAlert];
 }
orafaelreis
  • 2,855
  • 2
  • 28
  • 31