Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

iphone - UIWebView leaking

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.5k users

...