UIWebView memory leak when loaded from file instead of url?

I have a UIViewController that contains a UIWebView (OS 3.0). If I load it with the file data as soon as the back button is clicked and the view is rejected, I see an EXEC_BAD_ACCESS

error with the WebCore release: "SharedBuffer"

- (void)viewDidLoad {
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:fileName ofType:@"html"];   
    NSData *fileHtmlData = [NSData dataWithContentsOfFile:htmlFile];
    [webView loadData:fileHtmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];      
}

      

If I change the above to download on demand, everything works fine.

NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

      

In my controller dealloc, I release the webview with the following:

[webView setDelegate:nil];
[webView release];

      

The stack trace is below:

#2  0x359d34ae in WebCore::SharedBuffer::~SharedBuffer
#3  0x358fdab8 in WebCore::DocumentLoader::~DocumentLoader
#4  0x332d3c00 in WebDocumentLoaderMac::~WebDocumentLoaderMac
#5  0x358fec8c in WebCore::FrameLoader::detachFromParent
#6  0x332d8830 in -[WebView(WebPrivate) _close]
#7  0x332d8757 in -[WebView close]
#8  0x332d86db in -[WebView dealloc]
#9  0x35890719 in WebCoreObjCDeallocOnWebThreadImpl
#10 0x358d29ce in HandleWebThreadReleaseSource

      

Is there something else I need to do to prevent the leak / bad_access error?

+2


source to share


1 answer


It turns out that you need to follow the steps outlined here:

https://devforums.apple.com/message/10741#10741



in particular Jim's suggestion:

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [webView.delegate retain];

    // logic ...
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{   
     // logic ...

    [webView.delegate release];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
     // error logic ...
    [webView.delegate release];
}

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

    if (m_webView.loading)
    {
        [m_webView stopLoading];
    }

     // further logic ...
}

- (void)dealloc {
    m_webView.delegate = nil;
    [m_webView release];
   ...
    [super dealloc];
}

      

0


source







All Articles