UIWebView 泄漏 [英] UIWebView leaking

查看:14
本文介绍了UIWebView 泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展 UIViewController 并实现 UIWebViewDelegate 的类,如下所示:

I have a class that extends UIViewController and implements the UIWebViewDelegate, like this:

@interface TableViewController : UIViewController <UIWebViewDelegate, UIAlertViewDelegate>{
    UIWebView *articleWebView;
    NSString *link;
    UIActivityIndicatorView *activityIndicator;
    NSURL * safariUrl;
}

我正在尝试将网页加载到 uiwebview 中,一切正常,但我遇到了一些泄漏(如 GeneralBlock-56、GeneralBlock-1024 等),我无法找到它们的来源.
这就是我正在做的:

I'm trying to load a web page into the uiwebview, everything works fine, but i'm getting some leaks (like GeneralBlock-56, GeneralBlock-1024 etc) and i can't find out where they come from.
This is what i am doing:

- (void)viewDidLoad {
[super viewDidLoad];

if (articleWebView){
    [articleWebView loadHTMLString: @"" baseURL: nil];
    [articleWebView release];
    articleWebView = nil;
}

articleWebView = [[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,380)];
if (activityIndicator){

    [activityIndicator release];
    activityIndicator = nil;
}
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2-24, self.view.bounds.size.height/3-12, 50, 50)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.view addSubview:activityIndicator];


link = [link stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
link = [link stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
link = [(NSString*) CFURLCreateStringByAddingPercentEscapes (NULL, (CFStringRef)link, NULL,
                                                             NULL, kCFStringEncodingUTF8) autorelease];


NSURL *url = [NSURL URLWithString:link];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];   

[[self articleWebView] setDelegate: self];

[articleWebView loadRequest:requestObj];

[link release];

}

//////////////////////////////////////////////
-(void)webViewDidFinishLoad:(UIWebView *)webView{
if (activityIndicator){
    [activityIndicator stopAnimating];
    [activityIndicator release];
    activityIndicator = nil;
    self.view = articleWebView;

}
//////////////////////////////////////////////
- (void)viewDidUnload {
    [super viewDidUnload];
    [articleWebView loadHTMLString: @"" baseURL: nil];
    [self.articleWebView release];
    self.articleWebView = nil;
}
//////////////////////////////////////////////
- (void)dealloc {
    self.articleWebView.delegate=nil;
    [super dealloc];
}

我做错了什么?提前致谢.

What am i doing wrong? Thank you in advance.

推荐答案

在将 webView 委托设置为 nil 的同时,您应该要求您的 webView 停止加载.添加以下代码行,然后尝试:

Along with setting the webView delegate to nil, you should ask your webView to stop loading. Add the following lines of code and then try:

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

这篇关于UIWebView 泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆