开发者

UIWebView crashing only on second load

Ok I've got a dreaded EXC_BAD_ACCESS that I can't seem to track down but it only occurs on my second load of a UIWebView. I've searched and reviewed all the similar issues on SO and tried a few different approaches but I'm still struggling.

Here is the program flow that finally loads the WebView.

I have a Table View which lists videos. When a row is clicked a Detail View loads, with more info. about the video. Once the detail view is loaded, if the user clicks Play another view loads that has the WebView.

In this view, on viewDidLoad the NSURLRequest is created and passed to the WebView loadRequest.

The WebView loads fine and displays the webpage with the video I need. All of this is on a navigation controller so the user can click Back and returns to the previous detail view.

I am releasing the WebView in the dealloc and also stopping it from loading if it was still doing that. The WebView is obtained via an IBOutlet and is synthesized as (nonatomic,retain).

I have NSZombiesEnabled and didn't see any issues there. So when does it crash? If the user clicks Play a second time, the video View is created again and on the [webView loadRequest] - I get a EXC_BAD_ACCESS once the page has loaded.

Here is the kicker though, if I go back to the Table and pick another row (video), then I can load that WebView without any problems. It is only if I load the same video twice that it crashes.

The view that holds the WebView is released after being created and pushed on the nav. controller. I setup 开发者_如何学Goa ton of break points - everything (on the second load) is good right up until the webViewDidFinishLoad is called, whatever happens after that is causing the crash. I don't get a stack trace (or don't know how to find it) :)

Thanks!

Here is the code from the WebView view - some of this was based on other peoples findings of memory issues with UIWebView.

 - (void)viewDidLoad {

[super viewDidLoad];
self.title = @"Video Player";

NSURL *url = [NSURL URLWithString:video.url];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];  

}


- (void)viewDidUnload {
[super viewDidUnload];
[webView setDelegate:nil];
[self setWebView:nil];
}


- (void)dealloc {
[video release];
[super dealloc];
}


EDIT - If you don't want to read all the comments, here's what was going on :

The content was coming from you tube - using a different URL removed the crash :(


Hi,

1) What's with all the [self retain] and [self release] calls? You shouldn't need any of them - if you do then you're covering up a bug somewhere else in your code.

2) This just looks wrong -

[webView stopLoading];
if ([webView isLoading])
    [webView stopLoading];

if you have to put this there is a bug somewhere else - if calling stopLoading doesn't work then calling it again won't help!

3) Your entire loadRequest method can just be [webView loadRequest:request]; - you can fairly safely assume that if you tell the webview to load something, it will stop loading anything else.

4) I put [super viewDidLoad]; at the start not at the end of my viewDidLoad function - you don't know what else it sets up that you might need (though it might also do nothing - I just try to play it safe) 5) What's the property declaration in your .h file for the webView (it should be retain) (Assuming that it's created as a IBOutlet from a xib file)

6) In your viewDidUnload you need to remove your webview - set it's delegate to nil and call [self setWebView:nil]; otherwise you might leak it. (Assuming that it's created as a IBOutlet from a xib file)

I've just realised that this sounds very critical - it's not meant to be, we've all tried these things when debugging memory issues!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜