UIWebView crashes my app when dismissing the view…

Discussion in 'iOS Programming' started by cstromme, Aug 18, 2009.

  1. cstromme macrumors regular

    Joined:
    Feb 26, 2007
    #1
    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?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    It could be related of this:
    Code:
    webURL = URL;
    You should retain URL here, like so:
    Code:
    webURL = [URL retain];
     
  3. cstromme thread starter macrumors regular

    Joined:
    Feb 26, 2007
    #3
    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].
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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.
     
  5. cstromme thread starter macrumors regular

    Joined:
    Feb 26, 2007
    #5
    Been googling for a couple of hours now. :(

    Setting the delegate to nil didn't help.
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    What about removing the webview from its superview?
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
  8. cstromme thread starter macrumors regular

    Joined:
    Feb 26, 2007
    #8
    ^ 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.
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    What happens if the webview and its view controller are never released? Just keep them around.
     
  10. cstromme thread starter macrumors regular

    Joined:
    Feb 26, 2007
    #10
    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.
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    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.
     

Share This Page