2010年

[iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある このエントリーを含むはてなブックマーク

iPhone/iPod touch Add comments

UIWebView のインスタンスに delegate を指定している場合は以下のように release する前に delegate に nil をセットする必要があります。

- (void) viewDidLoad {
    webView = [[UIWebView alloc] init];
    webView.delegate = self;
    ....
}

- (void)dealloc {
    webView.delegate = nil;
    [webView release];
}


アップルのマニュアルにも以下のように書かれています。

Important: Before releasing an instance of UIWebView for which you have set a delegate, you must first set its delegate property to nil. This can be done, for example, in your dealloc method.

理由は UIWebView のロード処理が別スレッドで行われているために、release 後にデリゲートメソッドを呼ぶ可能性があるためだそうです。

以下のコードで実験してみました。UIWebView のインスタンスを生成後、loadRequest で読み込みます。その直後に UIWebView のインスタンスをリリースしてみました。そして、webView.delegate = nil を入れた時と入れないときの挙動を試してみるとわかりやすいです。

- (void)viewDidLoad {
    [super viewDidLoad];

    UIWebView* webView = [[UIWebView alloc] init];
    [self.view addSubview:webView];
    webView.delegate = self;
    NSURL* url = [NSURL URLWithString:@"http://example.com"];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];

    webView.delegate = nil;  // この行を入れた時と入れないときの挙動の違い
    [webView release];
}

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    NSLog(@"finish load");
}

delegate に nil を設定していないとデリゲートメソッドが呼ばれます。もし、このUIViewController のインスタンスが破棄されていればアプリが落ちてしまいます。

意図しないタイミングでデリゲートメソッドが呼ばれないために UIWebView のインスタンスをリリースする前にはデリゲートに nil をセットしなければいけないということです。

関連する投稿

16 Responses to “[iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある”

  1. syuhari Says:

    [blog] [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある http://is.gd/cgO5g

    This comment was originally posted on Twitter

  2. AppleWalker Says:

    Tips RT @syuhari: [blog] [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある http://is.gd/cgO5g

    This comment was originally posted on Twitter

  3. erguolinge Says:

    メモメモ “@syuhari: [blog] [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある http://is.gd/cgO5g”

    This comment was originally posted on Twitter

  4. bsiyo Says:

    iPhone: [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. : http://www.syuhari.jp/blog/archives/2137

    This comment was originally posted on Twitter

  5. WebBizNow Says:

    [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. UIWebView のインスタンスに… http://goo.gl/fb/wRSok

    This comment was originally posted on Twitter

  6. hatena_b Says:

    [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. http://bit.ly/cvBRbk

    This comment was originally posted on Twitter

  7. AppliNow Says:

    [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. UIWebView のインスタンスに… http://goo.gl/fb/oVETj

    This comment was originally posted on Twitter

  8. hatena_iphone Says:

    [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. http://bit.ly/daXEo3

    This comment was originally posted on Twitter

  9. hatebu_jyunkai Says:

    [はてブiPhone] [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt.: UIWebView のインスタンスに delegate を… http://bit.ly/abL0qs

    This comment was originally posted on Twitter

  10. shiroys Says:

    [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt.: UIWebView のインスタンスに delegate を指定している場合は以下の… http://bit.ly/cvBRbk

    This comment was originally posted on Twitter

  11. maito Says:

    参考になります。

  12. fumitaku Says:

    [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. http://bit.ly/cKbihW

    This comment was originally posted on Twitter

  13. matsuura Says:

    maito さん、ありがとうございます。参考になればなによりです。

  14. glassonion1 Says:

    めっちゃ参考になりました。RT @syuhari [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. http://bit.ly/9tS9eZ

    This comment was originally posted on Twitter

  15. syuhari Says:

    あざっす! RT @glassonion1: めっちゃ参考になりました。RT @syuhari [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある http://bit.ly/9tS9eZ

    This comment was originally posted on Twitter

  16. ifonejapan Says:

    From Blogs: [iPhone] UIWebView のリリース前に delegate に nil をセットする必要 …: UIWebView のインスタンスに delegate を指定している場合は以… http://bit.ly/daXEo3 #iphone

    This comment was originally posted on Twitter

Leave a Reply

Additional comments powered by BackType

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS ログイン