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 をセットする必要がある”
Leave a Reply
Additional comments powered by BackType
5月 20th, 2010 at 9:56 am
[blog] [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある http://is.gd/cgO5g
This comment was originally posted on Twitter
5月 20th, 2010 at 10:00 am
Tips RT @syuhari: [blog] [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある http://is.gd/cgO5g
This comment was originally posted on Twitter
5月 20th, 2010 at 10:10 am
メモメモ “@syuhari: [blog] [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある http://is.gd/cgO5g”
This comment was originally posted on Twitter
5月 20th, 2010 at 10:36 am
iPhone: [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. : http://www.syuhari.jp/blog/archives/2137
This comment was originally posted on Twitter
5月 20th, 2010 at 10:40 am
[iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. UIWebView のインスタンスに… http://goo.gl/fb/wRSok
This comment was originally posted on Twitter
5月 20th, 2010 at 10:48 am
[iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. http://bit.ly/cvBRbk
This comment was originally posted on Twitter
5月 20th, 2010 at 10:56 am
[iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. UIWebView のインスタンスに… http://goo.gl/fb/oVETj
This comment was originally posted on Twitter
5月 20th, 2010 at 11:02 am
[iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. http://bit.ly/daXEo3
This comment was originally posted on Twitter
5月 20th, 2010 at 11:15 am
[はてブiPhone] [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt.: UIWebView のインスタンスに delegate を… http://bit.ly/abL0qs
This comment was originally posted on Twitter
5月 20th, 2010 at 11:51 am
[iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt.: UIWebView のインスタンスに delegate を指定している場合は以下の… http://bit.ly/cvBRbk
This comment was originally posted on Twitter
5月 20th, 2010 at 11:53 am
参考になります。
5月 20th, 2010 at 11:56 am
[iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. http://bit.ly/cKbihW
This comment was originally posted on Twitter
5月 20th, 2010 at 11:58 am
maito さん、ありがとうございます。参考になればなによりです。
5月 20th, 2010 at 1:01 pm
めっちゃ参考になりました。RT @syuhari [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある | Sun Limited Mt. http://bit.ly/9tS9eZ
This comment was originally posted on Twitter
5月 20th, 2010 at 1:07 pm
あざっす! RT @glassonion1: めっちゃ参考になりました。RT @syuhari [iPhone] UIWebView のリリース前に delegate に nil をセットする必要がある http://bit.ly/9tS9eZ
This comment was originally posted on Twitter
5月 20th, 2010 at 6:06 pm
From Blogs: [iPhone] UIWebView のリリース前に delegate に nil をセットする必要 …: UIWebView のインスタンスに delegate を指定している場合は以… http://bit.ly/daXEo3 #iphone
This comment was originally posted on Twitter