Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cstromme

macrumors regular
Original poster
Feb 26, 2007
162
0
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:
Code:
#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:
Code:
#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?
 
It could be related of this:
Code:
webURL = URL;
You should retain URL here, like so:
Code:
webURL = [URL retain];
 
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].
 
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.
 
Been googling for a couple of hours now. :(

Setting the delegate to nil didn't help.
 
^ 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.
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.