UIAlertView でボタン押したら EXC_BAD_ACCESS?

ダイアログは表示できるんだけど、OKボタンを押すと EXC_BAD_ACCESS になる。単にダイアログ表示してるだけなのに?! と思ってたら autorelease が悪さしてた。あらら ^^;)

    UIAlertView *alertView;
    alertView = [[[UIAlertView alloc] initWithTitle:@"認証エラー" 
                                            message:@"サーバ認証に失敗しました" 
                                           delegate:nil cancelButtonTitle:nil 
                                  otherButtonTitles:@"OK", nil] autorelease];
    [alertView show];


UIAlertView はダイアログ表示して別スレッドでボタン操作を待つ。ボタン押したときにはリリースされてて EXC_BAD_ACCESS になったっぽい。

alertView を show したあとに release するのがお約束。

    UIAlertView *alertView;
    alertView = [[UIAlertView alloc] initWithTitle:@"認証エラー" 
                                           message:@"サーバ認証に失敗しました" 
                                          delegate:nil 
                                 cancelButtonTitle:nil 
                                 otherButtonTitles:@"OK", nil];
    
    [alertView show];
    [alertView release];


タイミング的には似たようなもんだろと思ってたけど、微妙に違うんだね。確かに alertView を autorelease って記述、見たことない。違いの認識が薄い分、気付かずにチョンボしてたわけだ。1つ勉強になった。 :-)

環境