memory bad access?

放肆的年华 提交于 2019-12-25 00:21:50

问题


How can this statement ever give me bad access?

myWebView = [[NewsWebViewController alloc] initWithNibName:@"NewsWebViewController" bundle:nil];
[[self.view.superview superview] addSubview:myWebView.view];

 if(myWebView!=nil) {
    [myWebView release];
    myWebView = nil;
}

Ive ran it through instruments and it crashes every time. myWebView is an ivar in my header file.

Any thoughts? Many thanks

Jules


回答1:


ok here is the actual issue. When I removed the webview it gets dealloc'd but I didnt set its delegate to nil. Therefore webViewDidFinishLoading etc was trying to access it and giving me bad access.

Thanks to all for your input.




回答2:


You should check that myWebView is nil before you add it's view to a subview. You do not need to set myWebView to nil after you release it.




回答3:


You are setting mywebview to nil after releasing so it crashes as the object no longer exists. Do it in this order:

if(myWebView!=nil) {

  myWebView = nil;
  [myWebView release];

}


来源:https://stackoverflow.com/questions/6267425/memory-bad-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!