簡體   English   中英

從文件而不是URL加載時發生UIWebView內存泄漏?

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

我有一個包含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錯誤嗎?

原來,您需要執行此處概述的步驟:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM