简体   繁体   English

从文件而不是URL加载时发生UIWebView内存泄漏?

[英]Memory leak UIWebView when loading from file instead of URL?

I have a UIViewController that contains a UIWebView (OS 3.0). 我有一个包含UIWebView(OS 3.0)的UIViewController。 If I load it with file data, as soon as the 'Back Button' is hit and the view is dismissed, I'm seeing EXEC_BAD_ACCESS error with WebCore object releasing 'SharedBuffer' 如果我用文件数据加载该文件,则一旦单击“后退按钮”并关闭了视图,就会看到EXEC_BAD_ACCESS错误,WebCore对象释放了“ 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 load via request, everything is fine. 如果我更改以上内容以通过请求加载,一切都很好。

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

In my controller's dealloc, I release webview with the following: 在我的控制器的dealloc中,使用以下内容释放webview:

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

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? 我还需要采取其他措施来防止泄漏/ bad_access错误吗?

Turns out that you need to do the steps outlined here: 原来,您需要执行此处概述的步骤:

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

specifically the suggestion made by Jim: 特别是吉姆的建议:

- (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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM