PDA

View Full Version : UIWebView crashes my app when dismissing the view…




cstromme
Aug 18, 2009, 03:41 PM
Seems UIWebView starts it's own thread when loading a webpage, and if I dismiss the view while this is going on the app will crash.

I've tried doing [webView stopLoading] before dismissing (presenting it as a modal view now), but that doesn't seem to help.

Here's my WebViewer.m:
#import "WebViewer.h"


@implementation WebViewer

@synthesize scalesPageToFit;
@synthesize webView;


- (id)initWithURL:(NSURL *)URL
{
if(self = [super init])
{
webURL = URL;
scalesPageToFit = YES;
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];

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

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

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}

- (IBAction)closeWebView
{
[webView stopLoading];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
NSLog(@"Dealloc called for WebViewer");
[super dealloc];
}

@end


And WebViewer.h:
#import <UIKit/UIKit.h>

@interface WebViewer : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *webView;
BOOL scalesPageToFit;
NSURL *webURL;
}

@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic) BOOL scalesPageToFit;

- (id)initWithURL:(NSURL *)URL;
- (IBAction)closeWebView;

@end

Anybody got a suggestion as to what to do here?



kainjow
Aug 18, 2009, 04:43 PM
It could be related of this:
webURL = URL;
You should retain URL here, like so:
webURL = [URL retain];

cstromme
Aug 18, 2009, 05:41 PM
Oh, sorry, I should have been more clear.

The thread that's actually fetching the webpage is running independently of the UIWebView, so if I dismiss the UIWebView the thread will try to message the object after it has been released.

So the problem really isn't in the source code that I posted, it's more a general problem with how UIWebView works when it fetches a webpage. I just posted the code to show my [webView stopLoading].

PhoneyDeveloper
Aug 18, 2009, 05:50 PM
There have been reports of this from time to time. You might google for it.

You could try setting the webview's delegate to nil.

cstromme
Aug 18, 2009, 06:12 PM
Been googling for a couple of hours now. :(

Setting the delegate to nil didn't help.

kainjow
Aug 18, 2009, 06:16 PM
What about removing the webview from its superview?

PhoneyDeveloper
Aug 18, 2009, 08:17 PM
This thread from a year ago talks about this

http://www.iphonedevsdk.com/forum/iphone-sdk-development/1032-cocoa-question-cleanup-delegates.html

Are you loading a local file or a remote file? In either case you might prefer to load the file into an NSData or NSString and then tell the web view to load that. That will reduce the amount of time the web view spends loading the file.

You also could retain the webview until it sends its webViewDidFinishLoad message and then release it.

cstromme
Aug 20, 2009, 03:16 PM
^ Followed everything in that thread, but I still have problems. I even set the dismissal of the modal dialog in the webView:didFailLoadWithError: method after doing a loadRequest:nil and the process in the thread that's loading the page is still running after that.

PhoneyDeveloper
Aug 20, 2009, 03:25 PM
What happens if the webview and its view controller are never released? Just keep them around.

cstromme
Aug 20, 2009, 04:14 PM
I thought I had tried that, but I hadn't. Tried it now and that worked. It will leave a bunch of unreleased objects though.

PhoneyDeveloper
Aug 20, 2009, 05:23 PM
Well at least that's a start.

There aught to be a time that those view controllers can be safely released so you don't need to leave a bunch of them around. It might be possible to reuse them otherwise.