从文件而不是URL加载时,内存泄漏UIWebView的?(Memory leak UIWebView

2019-08-04 10:16发布

我有一个包含一个UIWebView(OS 3.0)一个UIViewController。 如果我与文件数据,只要“后退按钮”加载它被击中,并认为被驳回,我看到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:@""]];      
}

如果我改变上述通过请求加载,一切都很好。

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

在我的控制器的dealloc,我释放的WebView有以下几点:

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

堆栈跟踪低于:

#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

有没有别的东西,我需要做的,以防止泄漏/ bad_access错误?

Answer 1:

原来,你需要做的在这里列出的步骤:

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

特别是建议由吉姆做:

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


文章来源: Memory leak UIWebView when loading from file instead of URL?