Nava Carmon
Nava Carmon

Reputation: 4533

webViewDidFinishLoad exception

I have a screen with a webView in my navigation controller stack and when I navigate from this view back to a previous before the page completely loaded I get a EXCEPTION_BAD_ACCESS. Seems, that the view with a webView being released before it comes to webViewDidFinishLoad function. My question is how to overcome this problem? I don't expect from the user to wait till the page loads... The code is very simple:

- (void) viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

NSURL *url = [NSURL URLWithString:storeUrl];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//Load the request in the UIWebView.
[browser loadRequest:requestObj];

}

TIA

Upvotes: 0

Views: 1107

Answers (1)

cduhn
cduhn

Reputation: 17918

I'm guessing that you have your browser.delegate property set to your custom UIViewController. When you hit the back button, this controller is popped off the navigation stack and dealloc'd, leaving your browser.delegate pointing at freed memory. When your page finishes loading, the UIWebView calls a method on its delegate, which no longer exists, so you get EXC_BAD_ACCESS.

To prevent bugs like this, any time you set an object's delegate property, make sure you set that property to nil in your dealloc, like this:

- (void)dealloc {
    self.browser.delegate = nil; // Prevents EXC_BAD_ACCESS
    self.browser = nil; // releases if @property (retain)
    [super dealloc];
}

Since nil silently swallows any messages it receives, this will prevent your EXC_BAD_ACCESS.

Upvotes: 1

Related Questions