views:

163

answers:

2

Hi,

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

A: 

It appears, that this question was answered before on SO

resolving crash in webView

Nava Carmon
+1  A: 

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.

cduhn
You're absolutely right, in addition there is a need to call for stopLoading on browser in order for this not to populate finishLoading event.
Nava Carmon