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

时间:2009-09-09 01:10:57

标签: iphone memory-management memory-leaks uiwebview

我有一个包含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错误吗?

1 个答案:

答案 0 :(得分:0)

原来您需要执行此处列出的步骤:

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

具体是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];
}