I have a variable set for my AppDelegate which stores the current URL that has been clicked on in a UIWebView. This variable is set in the shouldStartLoadWithRequest function, as below:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *val = [request.URL absoluteString];
currentURL = val;
return YES;
}
The problem is that later in the application, accessing currentURL causes a EXC_BAD_ACCESS error because the NSString has been destroyed. However, if I add a ‘retain’ to when the variable is being assigned, like:
NSString *val = [[request.URL absoluteString] retain];
Then this means that every time a page is viewed, a new string is retained which is never released, causing a memory leak. How can I later release all of these retained strings?
I am storing this value so that the page reloads when there is a problem loading the page.
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[ausWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]]
}
I’ll bet it’s something simple.
Assuming that currentURL retained property you should do:
and in your dealloc
or when you synthesized you retained property you only have to do in your shouldStartLoadWithRequest (still need to dealloc of course):