UIWebView leaking

2019-01-26 01:20发布

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

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.

2条回答
forever°为你锁心
2楼-- · 2019-01-26 01:45

If articeWebview is an IBOutlet why is it that you want to allocate it??

查看更多
可以哭但决不认输i
3楼-- · 2019-01-26 01:47

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];
}
查看更多
登录 后发表回答